简体   繁体   中英

Word replacement using regex if the target word is not a part of another word

I am working on regex expression for a word to be replaced if it is a standalone word and not a part of another word. For example the word "thing". If it is something , the substring "thing" should be ignored there, but if a word "thing" is preceded with a special character such as a dot or a bracket, I want it captured. Also I want the word captured if there is a bracket, dot, or comma (or any other non-alphanumeric character is there) after it.

In the string

Something is a thing , and one more thingy and ( thing and more thing

In the sentence above I highlighted the 3 words to be marked for replacement. I used the following Regex

\bthing\b

I tried out the above sentence on regex101.com and using this regex expression only the first word gotten highlighted. I understand that my regex would not capture (thing but I thought it would capture the last word in the sentence so that there would be at least 2 occurrences.

Can someone please help me modify my regex expression to capture all 3 occurences in the sentence above?

You were likely using the javascript regex, which returns after the first match is found. If you add the modifier g in the second box on regex101.com, it will find all matches.

This site is better for C# regex testing: http://derekslager.com/blog/posts/2007/09/a-better-dotnet-regular-expression-tester.ashx

If you code this in C# and use the 'Matches' method, it should match multiple times.

Regex regex = new Regex("\\bthing\\b");

foreach (Match match in regex.Matches(
    "Something is a thing, and one more thingy and (thing and more thing"))
{
    Console.WriteLine(match.Value);
}

Shorthand for alphanum [0-9A-Za-z] is [^\\W_]

Using a lookbehind and lookahead you'd get

(?<![^\\W_])thing(?![^\\W_])

Expanded

 (?<! [^\W_] )       # Not alphanum behind
 thing               # 'thing'
 (?! [^\W_] )        # Not alphanum ahedad

Matches the highlighted text

Something is a thing , and one more thingy and ( thing and more thing

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