help on regexp to extract words between ""

2 ビュー (過去 30 日間)
raym
raym 2019 年 9 月 24 日
回答済み: the cyclist 2019 年 9 月 24 日
aMajorLine = 'var _0x3f86=["\x72\x65\x64","\x67\x72\x65\x65\x6E","\x62\x6C\x75\x65"]';
[a,b] = regexp(aMajorLine,'var _0x3f86=\[\"([\w\\]+)\",?]+\]','tokens')
I want to extract the words inside "". The results is empty. Is there something wrong?

採用された回答

the cyclist
the cyclist 2019 年 9 月 24 日
I find it difficult to debug someone else's regexp. :-(
Before seeing @Ruger28's elegant solution, I would have done
matches = regexp(aMajorLine,'"[\w*,\\]*"','match')
matches = regexprep(matches,'"','');
Note that I first pull out with the double-quote characters, then strip them out in the second line. It is possible to do this in one regexp step instead, using "lookaround" assertions, but I find that less easy to understand.

その他の回答 (1 件)

Ruger28
Ruger28 2019 年 9 月 24 日
if using R2016b or later
aMajorLine = 'var _0x3f86=["\x72\x65\x64","\x67\x72\x65\x65\x6E","\x62\x6C\x75\x65"]';
extractedText = extractBetween(aMajorLine,'"','"');
which gives:
extractedText =
3×1 cell array
{'\x72\x65\x64' }
{'\x67\x72\x65\x65\x6E'}
{'\x62\x6C\x75\x65' }
  3 件のコメント
Ruger28
Ruger28 2019 年 9 月 24 日
It is pretty amazing! There is also extractBefore and extractAfter, and they are imensly useful when convoluted regexp's are normally needed. Only thing is that they are 2016 or later.
raym
raym 2019 年 9 月 24 日
Unlucky I'm using R2016a

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

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by