regexp shows an error while reading a space in text file.

1 回表示 (過去 30 日間)
Aakriti Srivastava
Aakriti Srivastava 2022 年 7 月 25 日
編集済み: Aakriti Srivastava 2022 年 7 月 25 日
This is the section that I want to read using regexp from port 12 to port 22 as
PORT 23 P=1 Z=50
PORT 12 P=2 Z=50 PIN_ID=CB7TX
PORT 13 P=3 Z=50 PIN_ID=CB7RX
PORT 14 P=4 Z=50 PIN_ID=CB66TX
PORT 15 P=5 Z=50 PIN_ID=CB25TX
PORT 16 P=6 Z=50 PIN_ID=CB25RX
PORT 22 P=7 Z=50 PIN_ID=CB66RX
This is the code I am using
[port_tokens2,port_match3] = regexp(data,'PORT\s+(\d+)\s+(\w+=\d)+\s+\w+=\d+\s','tokens','match')
Result I got
port_match3 =
1×7 cell array
Columns 1 through 5
{'PORT 23 P=1 Z=5…'} {'PORT 12 P=2 Z=50 '} {'PORT 13 P=3 Z=50 '} {'PORT 14 P=4 Z=50 '} {'PORT 15 P=5 Z=50 '}
Columns 6 through 7
{'PORT 16 P=6 Z=50 '} {'PORT 22 P=7 Z=50 '}
But I want to read PIN_ID = CBXXXX also so I used this expression
[port_tokens2,port_match3] = regexp(data,'PORT\s+(\d+)\s+(\w+=\d)+\s+\w+=\d+\s\w+=w+','tokens','match')
This is the result I got
port_match3 =
0×0 empty cell array
Would anyone please let me know what is wrong with my expression that I am not able to read the whole sentence.
  3 件のコメント
Aakriti Srivastava
Aakriti Srivastava 2022 年 7 月 25 日
Hi yaah I tried with that one also but it didnt work i.e. the additional space but it didnt work any other probability or erorr in my expression u can think of because I tried all possible way to read them out but no success and I really need to read this. Thanks
Walter Roberson
Walter Roberson 2022 年 7 月 25 日
I would debug by extending one bit at a time to see exactly where it is breaking

サインインしてコメントする。

採用された回答

VBBV
VBBV 2022 年 7 月 25 日
ss = 'PORT 12 P=2 Z=50 PIN_ID=CB7TX'
[port_tokens2,port_match3] = regexp(ss,'PORT\s+(\d+)\s+(\w+=\d)+\s+\w+=\d+\s+(\w*=\w*)','tokens','match')
You can try this

その他の回答 (1 件)

Jakob Hölzl
Jakob Hölzl 2022 年 7 月 25 日
Hi,
regex101.com is a good website for such cases.
When I checked it there, the problem seemed to be that the last w+ should be \w+ to match the value of the PIN_IDs
Kind regards,
Jakob
  2 件のコメント
Aakriti Srivastava
Aakriti Srivastava 2022 年 7 月 25 日
編集済み: Aakriti Srivastava 2022 年 7 月 25 日
Hi
Jakob I really appreciate this debugging platform thanks for info.
Jakob Hölzl
Jakob Hölzl 2022 年 7 月 25 日
you're welcome!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

タグ

製品


リリース

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by