I want to find the word immediately after a particular word in a string using a regex. For example, if the word is 'my',
"This is my prayer"- prayer
"this is my book()"- book
Is it possible using regex?
To run a “whole words only” search using a regular expression, simply place the word between two word boundaries, as we did with ‹ \bcat\b ›. The first ‹ \b › requires the ‹ c › to occur at the very start of the string, or after a nonword character.
\f stands for form feed, which is a special character used to instruct the printer to start a new page.
3.6. (? i) makes the regex case insensitive. (? s) for "single line mode" makes the dot match all characters, including line breaks.
* - means "0 or more instances of the preceding regex token"
The regex would be
(?<=\bmy\s+)\p{L}+
\p{L}+
is a sequence of letters. The \p{L}
is a Unicode code point with the property "Letter", so it matches a letter in any language.
(?<=\bmy\s+)
is a lookbehind assertion, that ensures thet word "my" is before
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With