Skip to main content

Regex get string between matching strings

Friends

If we want to get exact value between two matching strings.

Example : 

Statement: Shreeyansh Shlok is a good boy
Question : Get value (adjective) between a and boy in above statement

a\s(.*)\sboy
Output : good

Please note, we wanted to catch only the adjective without spaces, so we included \s (regex for matching space) after and before the strings. In case we don't care about space we can use following instead.

a(.*)boy
Output : good

We can try above or learn more regex here : Regex101

References : Stackoverflow help

Comments