正则表达式EX pression到全字匹配? [英] Regex expression to match whole word ?

查看:143
本文介绍了正则表达式EX pression到全字匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在引用我的问题正则表达式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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆