正则表达式中的自定义单词边界 [英] Custom word boundaries in regular expression

查看:53
本文介绍了正则表达式中的自定义单词边界的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用正则表达式匹配单词,但遗憾的是单词边界字符 (\b) 没有包含足够的字符,因此我想添加更多字符.(在这种情况下,+"字符)

I am trying to match words using a regular expression, but sadly the word boundary character (\b) does not include enough characters for my taste, so I want to add more. (in that precise case, the "+" character)

这是我曾经拥有的(它是 C#,但不是很相关):

Here is what I used to have (it is C# but not very relevant) :

string expression = Regex.Escape(word);
Regex regExp = new Regex(@"\b" + expression + @"\b", RegexOptions.IgnoreCase);

这个特定的正则表达式与C++"不匹配,我认为它真的很糟糕.所以我尝试在字符类中使用 \w 字符,以及 + 字符:

This particular regex did not match "C++" and I thought it was a real bummer. So I tried using the \w character in a character class that way, along with the + character :

string expression = Regex.Escape(word);
Regex regExp = new Regex(@"(?![\w\+])" + expression + @"(?![\w\+])", RegexOptions.IgnoreCase);

但是现在,没有任何东西匹配...是不是我遗漏了什么?

But now, nothing gets matched... is there something I am missing?

推荐答案

(字符类中无需转义+)

问题是您首先使用负向后视,而您应该使用负向后视.试试:

The problem is that you use a negative lookahead first whereas you should use a negative lookbehind. Try:

@"(?<![\w+])" + expression + @"(?![\w+])"

这篇关于正则表达式中的自定义单词边界的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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