正则表达式匹配所有单词,除了给定名单 [英] Regex to match all words except a given list

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

问题描述

我想写一个替代常规EX pression包围在引号中的所有单词,除了单词AND,OR和NOT。

我已经尝试了以下内容的EX pression比赛的部分:

 (小于字GT; [A-Z0-9] +?)(我?)(小于?!和|不是|和)
 

 (我?)(小于字GT; [A-Z0-9] +)(?!和|不是|和)
 

但既不工作。更换前pression很简单,目前围绕着所有单词。

 $ {}字
 

于是

  

这和这不是那

变为

  

这和这个不,即

解决方案

这是一个有点脏,但它的作品:

 (小于\ B(:?!?和|和|未))(?!(?:和|和|没有)\ B)\ b
 

在简单的英语,这是和,或,或不匹配任何单词边界没有$ P $由pceded,而不是跟随。它只匹配整个单词,例如单词沙后的位置不会是一场比赛,只是因为它是由pceded $ P $和

在前面的空间中的或在零宽度查找背后断言是必要使之成为固定长度的查找后面。尝试,如果已经解决您的问题。

编辑:适用于字符串除了词AND,OR和NOT。作为一个单引号全局替换,这将返回:

 '除''的''字'AND,OR和NOT。
 

I am trying to write a replacement regular expression to surround all words in quotes except the words AND, OR and NOT.

I have tried the following for the match part of the expression:

(?i)(?<word>[a-z0-9]+)(?<!and|not|or)

and

(?i)(?<word>[a-z0-9]+)(?!and|not|or)

but neither work. The replacement expression is simple and currently surrounds all words.

"${word}"

So

This and This not That

becomes

"This" and "This" not "That"

解决方案

This is a little dirty, but it works:

(?<!\b(?:and| or|not))\b(?!(?:and|or|not)\b)

In plain English, this matches any word boundary not preceded by and not followed by "and", "or", or "not". It matches whole words only, e.g. the position after the word "sand" would not be a match just because it is preceded by "and".

The space in front of the "or" in the zero-width look-behind assertion is necessary to make it a fixed length look-behind. Try if that already solves your problem.

EDIT: Applied to the string "except the words AND, OR and NOT." as a global replace with single quotes, this returns:

'except' 'the' 'words' AND, OR and NOT.

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

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