Quantcast
Viewing latest article 2
Browse Latest Browse All 14

Answer by Daksh Gargas for What is a word boundary in regex?

I would like to explain Alan Moore's answer

A word boundary is a position that is either preceded by a word character and not followed by one or followed by a word character and not preceded by one.

Suppose I have a string "This is a cat, and she's awesome", and I want to replace all occurrences of the letter 'a' only if this letter ('a') exists at the "Boundary of a word",

In other words: the letter a inside 'cat' should not be replaced.

So I'll perform regex (in Python) as

re.sub(r"\ba","e", myString.strip()) //replace a with e

Therefore,

Input; Output

This is a cat and she's awesome

This is e cat end she's ewesome


Viewing latest article 2
Browse Latest Browse All 14

Trending Articles