Regexp matches only as few characters as possible

3 次查看(过去 30 天)
Hey,
I'm trying to figure out how can I find expression which matches following strings:
strToBeChecked{1} = 'XYZ_500mmsV_xxx';
strToBeChecked{2}= 'XYZ_500msV_xxx';
regexpi(strToBeChecked{1},'(?<val>\d{2,5})[_- ]?(?<dim>m{1,2}sV)','names');
regexpi stores only msV as dim instead of mmsV. While
regexpi(strToBeChecked{2},'(?<val>\d{2,5})[_- ]?(?<dim>m{1,2}sV)','names');
acts just like it should (stores msV as dim).
Hope anyone knows workaround for this :)
Would help me a lot !

采纳的回答

Walter Roberson
Walter Roberson 2020-5-18
[_- ]
is a range match specification. It matches any one character that is between '_' and ' ' (space), inclusive. And it happens that you can specify the range backwards, so for example [d-a] is the same as [a-d] both matching any of 'a', 'b', 'c', 'd'.
The characters that are in the range of space to underscore are ' !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_ ' -- however you used regexpi so it is case insensitive, so it also matches lower-case alphabetics.
If you wanted to match any one of '_', '-' or space, then you use a syntax oddity: '-' is not recognized as a range indication if in any of the forms [LETTERS-] or [-LETTERS] or [^LETTERS-] or [^-LETTERS] so you could code [_ -] instead of [_- ]
  1 个评论
Mate D
Mate D 2020-5-18
thanks :) switched [_- ] to [_ -] and it worked instantly.
I forgot that "-" has special meaning inside [ ].

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by