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

Popular posts from this blog

IEnumerable Vs IEnumerator

Friends This is one of most basic things to understand in .Net Collections (not sure if it is same in Java or other langs). IEnumerable is superclass of all we use Lists, Dictionaries, HashTables and all others.

Lambda expressions basics

Friends I have always mentioned and maintained that I am a fan of Shiv Prasad Koirala. Learnt so many topics reading his articles and watching his recorded videos.  Following is a gem. It is mostly about 3 Generic Delegates namely Func, Action & Predicate. But as usual he discusses right from start that is Delegates. Its a fun watch. At last even he gives a small glimpse of Expression Trees. A must watch for Beginners and Intermediate programmers. Talk is cheap, so let's jump to