正则表达式匹配整个字与特殊字符不起作用? [英] Regex expression to match whole word with special characters not working ?

查看:159
本文介绍了正则表达式匹配整个字与特殊字符不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在经历这个问题
C#,Regex.Match整个单词

它表示匹配全字使用\bpattern\b
对于没有任何特殊字符的匹配整个单词,这是适用的仅限字词

It says for match whole word use "\bpattern\b" This works fine for match whole word without any special characters since it is meant for word characters only!

我还需要一个表达式来匹配特殊字符。我的代码如下

I need an expression to match words with special characters also. My code is as follows

class Program
{
    static void Main(string[] args)
    {
        string str = Regex.Escape("Hi temp% dkfsfdf hi");
        string pattern = Regex.Escape("temp%");
        var matches = Regex.Matches(str, "\\b" + pattern + "\\b" , RegexOptions.IgnoreCase);
        int count = matches.Count;
    }
}

但由于%而失败。我们有任何解决方法吗?
可以有其他特殊字符,如'空格','(',')'等等

But it fails because of %. Do we have any workaround for this? There can be other special characters like 'space','(',')', etc

推荐答案

这个问题的答案可以在这里找到

The answer to this Question can be found here

正则表达式匹配整个单词

感谢您的所有答案!

这篇关于正则表达式匹配整个字与特殊字符不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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