[英]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): 当我尝试测试我的正则表达式时(方法转储来自linqpad以在控制台上显示):
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. 它应匹配以“ a”开头且在单词末尾具有x个字母数字字符的单词。
But unfortunately the regex above matches. 但是不幸的是上面的正则表达式匹配。
My understanding of the regex: 我对正则表达式的理解:
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. 它仅匹配您字符串的a
。
Since a
is a word character and ^
is not a word character, the empty string between them defines the word boundary. 由于a
是单词字符,而^
不是单词字符,因此它们之间的空字符串定义了单词边界。 (for \\b
) (对于\\b
)
In your case the a
is matched because it is followed directly by the word boundary as mentioned above. 在您的情况下, a
是匹配的,因为如上所述,其后紧跟单词边界。 The reason is that the *
matches zero or more characters of the preceding token. 原因是*
与前面的令牌的零个或多个字符匹配。
Depending if your x
should be 1 or more tokens instead of 0 or more tokens, you need to change to \\ba\\w+\\b
. 根据您的x
是1个或多个标记而不是0个或多个标记,您需要更改为\\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. 正则表达式仅匹配“ a”,但这仍然是匹配项,并且成功为真。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.