正则表达式和冒号(:) [英] Regex and the colon (:)
本文介绍了正则表达式和冒号(:)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有下面的代码。这样做是为了检测整个单词。
I have the following code. The idea is to detect whole words.
bool contains = Regex.IsMatch("Hello1 Hello2", @"\bHello\b"); // yields false
bool contains = Regex.IsMatch("Hello Hello2", @"\bHello\b"); // yields true
bool contains = Regex.IsMatch("Hello: Hello2", @"\bHello\b"); **// yields true, but should yield false**
似乎正则表达式是忽略了结肠。我如何修改代码,这样最后一行将返回假的?
Seems that Regex is ignoring the colon. How can I modify the code such that the last line will return false?
推荐答案
\b
表示单词边界。 :
不是任何单词的一部分,所以该表达式为true
\b
means "word boundary". :
is not part of any word, so the expression is true.
也许你想这样的表达式:
Maybe you want an expression like this:
(^|\s)Hello(\s|$)
这意味着:字符串Hello,由表达式或空白,任开始之前和之后的任何一个结束表达式或空白。的
这篇关于正则表达式和冒号(:)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文