简体   繁体   English

.NET正则表达式中的锚点

[英]Anchors in .NET regular expressions

This is a question about the answer given in the question Check a string to see if all characters are hexadecimal values . 这是关于检查字符串以查看所有字符是否为十六进制值的问题中给出的答案的问题。

The proposed regular expression is the following: 拟议的正则表达式如下:

\A\b[0-9a-fA-F]+\b\Z

Now, \\A and \\Z seem to be the equivalent to ^ and $ respectively. 现在, \\A\\Z似乎分别相当于^$ \\Z behaves differently, in that it allows a newline after it when matching (this might or might not be intended). \\Z行为有所不同,因为它在匹配时允许换行后(这可能是也可能不是)。

What I don't understand is why the \\b "match at word boundary" anchor is used. 我不明白为什么使用\\b “在字边界匹配”锚点。 Isn't the beginning/end of a string always a word boundary? 字符串的开头/结尾始终不是单词边界吗?

Ultimately, the regex could be rewritten as ^[0-9a-fA-F]$ with the same behavior (ignoring the trailing \\n issue). 最终,正则表达式可以被重写为具有相同行为的^[0-9a-fA-F]$ (忽略尾随\\n问题)。 Am I missing something? 我错过了什么吗? Is using \\b required for some weird edge case? 使用\\b需要一些奇怪的边缘情况?

Test cases: 测试用例:

123ABC -> true
123def -> Returns true
123g -> Returns false

The word boundary \\b matches between non-word and word characters, and also at the start of the string if the first character is a word character, and at the end if the last character is a word character. 单词boundary \\b在非单词和单词字符之间匹配,如果第一个字符是单词字符,也在字符串的开头匹配,如果最后一个字符是单词字符,则在末尾匹配。

Thus, \\A\\b[0-9a-fA-F]+\\b\\Z is equal to \\A[0-9a-fA-F]+\\Z because all the characters in the string must be word characters ( [0-9] digits or [a-fA-F] letters) for the pattern to match it . 因此, \\A\\b[0-9a-fA-F]+\\b\\Z等于\\A[0-9a-fA-F]+\\Z因为字符串中的所有字符必须是单词字符( [0-9]数字或[a-fA-F]字母)表示模式匹配它

It would be a different story in this case: \\A\\b[0-9a-fA-F-]+\\b\\Z that would only match strings with word characters at the beginning and end. 在这种情况下,它将是一个不同的故事: \\A\\b[0-9a-fA-F-]+\\b\\Z只能匹配字符串开头和结尾的字符串。

Use \\z to match a whole string, with no \\n allowed at the end. 使用\\z匹配整个字符串,最后不允许\\n

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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