Azzera filtri
Azzera filtri

Regexp matches only as few characters as possible

3 visualizzazioni (ultimi 30 giorni)
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 !

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by