Regexp matches only as few characters as possible

조회 수: 1 (최근 30일)
Mate D
Mate D 2020년 5월 18일
댓글: Mate D 2020년 5월 18일
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