正则表达式来表达整个单词有特殊字符不工作匹配? [英] Regex expression to match whole word with special characters not working ?

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

问题描述

我整个单词经历这个问题
Regex.Match

I was going through this question Regex.Match whole words

报告说,为全字匹配使用\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天全站免登陆