Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Build regex with unknown chars/words in String between to known chars/words

I am relatively new to regex building and I am stuck at an example, where I am not sure if that is even possible. At least I didn’t find a working solution with regex101 or a similiar question on SO.

What I am trying to do:

> String1 = "NOTE: This is an examplary note about apples and other fruits."
> String2 = "NOTE: Here we have a simple note about bananas and other fruits." 
> String3 = "NOTE: note about cherrys and other fruits."

For all these strings the regex should catch the specific fruit (apples, bananas, cherrys), which is not the problem.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The string always begins with the same word/chars (NOTE:), afterwards there could be any amount of chars until another group of words that is always the same (note about). Then the desired catch comes and another group of words that is always the same. So my problem is the regex for the "unknown" part between "NOTE:" and "note about …". Is that even possible to put in a regex?

I hope you understand what I mean. Please respond if any clarification is needed here.

>Solution :

You could catch everything lazily to get the unknown part ?.*. So the regex could look like:

NOTE: ?.* note about (\w+) and other fruits\.

https://regex101.com/r/HKkyOq/1

Let me know if this answered your question.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading