Will REGEXP recognize specific characters within parenthesis?

4 ビュー (過去 30 日間)
Brad
Brad 2017 年 5 月 9 日
コメント済み: Brad 2018 年 3 月 9 日
I'm attempting to use REGEXP to match a specific string within a set of parenthesis. The code I'm using is as follows;
str = '(0:20)';
exp = '([\P\0\D\d])';
tokens = regexp(str, exp, 'tokens');
b = reshape([tokens{:}], [ ], 1).';
Data = cell2mat(b);
When executed, Data contains a 1 x 6 array of char: (0:20)
I'm having difficulty in making the expression sensitive to the contents of this array. Specifically, I'm only interested in the data if the value to the left of the colon is a zero.
Is this possible when using \P as part of the expression?
  1 件のコメント
Stephen23
Stephen23 2017 年 5 月 10 日
The MATLAB regular expression syntax is documented here:
and it does not include any operator '\P'. In any case you have not given us any information about what data you want to obtain from the string: is it the second number value, or all of the string within parentheses, or both number values... is the colon always present, or could it be a concatenation operator instead? You state that "only interested in the data if the value to the left of the colon is a zero", but what is "the data"? Even parentheses could be data. It is only worth starting to write a regular expression once you clearly specify the requirements it has to meet.
You could start with something like this, which returns the number the follows '0:'
>> str = '(0:20)';
>> regexp(str,'(?<=0:)\d+','match','once')
ans =
20
Bonus You might like to download and try my FEX submission:
which lets you interactively develop regular expressions and see the outputs change as you type/alter your expression.

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

採用された回答

Walter Roberson
Walter Roberson 2017 年 5 月 9 日
MATLAB's regexp does not know anything about unicode categories. Also, \0 is not a valid unicode category name.
It looks to me as if what you are looking for is:
'\(0:\d+\)'
  1 件のコメント
Brad
Brad 2018 年 3 月 9 日
Walter, after looking into this more, you are correct. My search pattern was a last ditch effort to get the result that Stephen came to using his code. These regular expressions are an ongoing learning experience!!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by