正则表达式EX pression到全字匹配? [英] Regex expression to match whole word ?
问题描述
在引用我的问题正则表达式EX pression到全字匹配有特殊字符不工作?,
我这说的答案
@"(?<=^|\s)" + pattern + @"(?=\s|$)"
这工作正常,除1例的所有情况。它失败时有空间的格局。
This works fine for all cases except 1 case. It fails when there is space in the pattern.
假设字符串你好,这是计算器的模式是本,那么它说没有匹配。出现这种情况是因为图案的实际字符串后,一个空的空间。
Assume string is "Hi this is stackoverflow" and pattern is "this " , then it says no matches. This happens because of an empty space after the actual string in pattern.
我们如何处理呢?理想的情况来讲,应该说一场比赛找到了!
推荐答案
试试这个
(?:(?<=^|\s)(?=\S)|(?<=\S|^)(?=\s))this (?:(?<=\S)(?=\s|$)|(?<=\s)(?=\S|$))
请参阅它这里Regexr
这也将适用于与一个空格开头的模式。
This will also work for pattern that starts with a whitespace.
基本上,我在做什么是定义一个定制的字的边界。但它不是在真实的一个 \ W =&GT; \ W
或 \ W =&GT; \ W
变化,其真正在 \ S =&GT; \ S
或 \ S =&GT;!\ S
变化
Basically, what I am doing is to define a custom "word" boundary. But it is not true on a \W=>\w
or a \w=>\W
change, its true on a \S=>\s
or a \s=>\S
change!
下面是在C#中的一个例子:
string str = "Hi this is stackoverflow";
string pattern = Regex.Escape("this");
MatchCollection result = Regex.Matches(str, @"(?:(?<=^|\s)(?=\S)|(?<=\S|^)(?=\s))" + pattern + @"(?:(?<=\S)(?=\s|$)|(?<=\s)(?=\S|$))", RegexOptions.IgnoreCase);
Console.WriteLine("Amount of matches: " + result.Count);
foreach (Match m in result)
{
Console.WriteLine("Matched: " + result[0]);
}
Console.ReadLine();
更新:
这个空白的边界可以做到更普遍,从而使在图案两侧是一样的EX pression,像这样
This "Whitespace" boundary can be done more general, so that on each side of the pattern is the same expression, like this
(?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$))
在C#:</ P>
In c#:
MatchCollection result = Regex.Matches(str, @"(?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$))" + pattern + @"(?:(?<=^|\s)(?=\S|$)|(?<=^|\S)(?=\s|$))", RegexOptions.IgnoreCase);
这篇关于正则表达式EX pression到全字匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!