Regular Expression help (is a line end token being matched in the middle of a string?)

4 visualizaciones (últimos 30 días)
Hi all,
I've hit a lack of understanding on regular expressions and I'm hoping someone can help
If I start with a simple enough string:
str = 'Serial Number: NEO1B39100092'
I first check for the existence of my keyword:
keyword = '(\W|^)serial number:*(\W|$)+'
and then try to find its value with:
expr = ['(?<=', keyword, ')[^\s]+'] % N.B. using [^\s] as a template for '[^', delimiters ']'
for comparison, I'll use the keyword without the line end anchor
keyword2 = '(\W|^)serial number:*(\W)+'
expr2 = ['(?<=', keyword2, ')[^\s]+']
Now if I use regexpi to test the two expressions
value = regexpi(str, expr, 'match', 'once')
value2 = regexpi(str, expr2, 'match', 'once')
I see that
value = ':',
while
value2 = 'NEO1B39100092'
My take on this is that the line anchor '$' from the original keyword is being match with the letter r somehow. For it to return ' : ' the character before ' : ' must match (\W|$)+ , and it can't be the \W because expr2 gives the expected result.
Can anyone shed some light on this for me?
Thanks for any help, Andrew

Respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by