Regex to match everything from one word to another

We have provided a complete guide on the regex that select anything from one specific word to another. We covered all the possible conditions and limitation that you might probably face in this situation. SO, stick to the tutorial till very end and you’ll get all the answer to the questions that in your mind.

There is a simple regex we provided is given below. But, the regex is with certain conditions and limitations.


Condition 1 :

(?=Text1)(.*)(?<=Text2)

The above regex selects everything that starts from Text1 and ending till Text2 including both the words. If you want the exact condition then its the correct regex for you.

But if you want to not include Text1 & Text2 words in the selection, then you need some small changes in the regex as mentioned below.


Condition 2:

(?<=Text1)(.*)(?=Text2)

This regex will select everything that starts after Text1 & end before Text2.


Limitations

Remember: all the words you put into the regex are case sensitive.

It will not consider anything after enter key. So, if your content has enter key pressed, then you have to use \n to include enter key.


Related Regex

1. Regex to select everything before a specific word. This regex term can be as including that word or excluding that word.

(.*)(?<=Text2)

Regex to select everything before a specific word including that word

(.*)(?=Text2)

Regex to select everything before a specific word excluding that word

There is only difference between forehead arrow (<)

 

2. Regex to select everything after a specific word.

(?=Text1)(.*)

(?<=Text1)(.*)


Hope, you got the solution for your all the problem. If you have any questions or doubt about this topic then ask freely in the comment section. We are always available to help you.

Leave a Comment