Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to find the word immediately after a particular word

Tags:

c#

.net

regex

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?

like image 889
Shubhang Avatar asked Jan 18 '13 09:01

Shubhang


People also ask

How do you find a specific word in a regular expression?

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.

What does \f mean in regex?

\f stands for form feed, which is a special character used to instruct the printer to start a new page.

What does regex (? S match?

3.6. (? i) makes the regex case insensitive. (? s) for "single line mode" makes the dot match all characters, including line breaks.

Why * is used in regex?

* - means "0 or more instances of the preceding regex token"


1 Answers

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

like image 73
stema Avatar answered Oct 07 '22 20:10

stema