Regular Expression help (is a line end token being matched in the middle of a string?)
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!