Borrar filtros
Borrar filtros

Regexp matches only as few characters as possible

3 visualizaciones (últimos 30 días)
Mate D
Mate D el 18 de Mayo de 2020
Comentada: Mate D el 18 de Mayo de 2020
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 !

Respuesta aceptada

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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by