简体   繁体   English

需要一个正则表达式来匹配单个字符或单词,但不应该匹配子字符串

[英]Need a regex to match a single character or word but should not match substrings

I have the following sample text: 我有以下示例文本:

I want to replace all instances of ; 我想替换所有的实例; , : , , , . :,. , and , a , an , the with pipe | andaanthe与管| symbol. 符号。

So the output should be something like: 所以输出应该是这样的:

I tried with the following regex but i am not getting a generic regex which matches for all: 我尝试使用以下正则表达式,但未获得与所有匹配的通用正则表达式:

"\/(^|\\W);($|\\W)\/",
"\/(^|\\W):($|\\W)\/",
"\/(^|\\W),($|\\W)\/",
"\/(^|\\W).($|\\W)\/",
"\/(^|\\W)and($|\\W)\/",
"\/(^|\\W)a($|\\W)\/",
"\/(^|\\W)an($|\\W)\/",
"\/(^|\\W)the($|\\W)\/",
"\/(^|\\W)said($|\\W)\/",

Also tried: 还尝试了:

(?<=\s)(;.)
(?<=\s)(:.)
(?<=\s)(,.)
(?<=\s)(..)
(?<=\s)(an.)
(?<=\s)(and.)

But does not work, please help. 但是不起作用,请帮忙。 Please note a search for a should match the portion 请注意,搜索的a应与该部分匹配

with a light emitting 发光

but should not match 匹配

extraction 萃取

. Similar behavior required for others. 其他人也需要类似的行为。

Although there are some ambiguous cases, by using below regex you are able to match those characters. 尽管存在一些模棱两可的情况,但是通过使用下面的正则表达式,您可以匹配那些字符。 Be careful about word boundary and non-word boundary meta-characters: 注意单词边界和非单词边界元字符:

[;.,:]\B|\b(?:an?d?|the)\b

Live demo 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM