简体   繁体   中英

Regular expression matches special character

I'm following this tutorial .

When I was trying to test my regular expression (The method dump is from linqpad to display it on the console):

Regex.Match("a^7lowah", @"\ba\w*\b").Success.Dump();

It should match a word that starts with an "a" and has x amount of alphanumeric characters to the end of the word.

But unfortunately the regex above matches.

My understanding of the regex:

  • "\\b" (begin of the word)
  • "a" (just the letter a)
  • "\\w" (alphanumeric character)
  • "*" (repeat previous term)
  • "\\b" (end of the word)

What am I doing wrong?

Yes, the regex will match.

Pattern: \ba\w*\b
String: a^7lowah

The * means "zero or more".

So this will be the match:

在此处输入图片说明

As you can see, no word characters are matched, but because you're quantifying "zero or more", it does not matter - our pointer skips over that part of the construct, and are already possible in asserting a word boundary.

You might want to change * to + instead.

Read also:

It matches only the a of your string.

Since a is a word character and ^ is not a word character, the empty string between them defines the word boundary. (for \\b )

In your case the a is matched because it is followed directly by the word boundary as mentioned above. The reason is that the * matches zero or more characters of the preceding token.

See here .

Depending if your x should be 1 or more tokens instead of 0 or more tokens, you need to change to \\ba\\w+\\b .

The problem is not in your Regexp, it's in your interpretation of success. The regexp matches the "a" only, but that is still a match and Success will be true.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM