Regular Expression help (is a line end token being matched in the middle of a string?)
조회 수: 2 (최근 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!