Clear Filters
Clear Filters

Regexp matches only as few characters as possible

3 views (last 30 days)
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 !

Accepted Answer

Walter Roberson
Walter Roberson on 18 May 2020
[_- ]
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 Comment
Mate D
Mate D on 18 May 2020
thanks :) switched [_- ] to [_ -] and it worked instantly.
I forgot that "-" has special meaning inside [ ].

Sign in to comment.

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!