简体   繁体   中英

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 | 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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