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

Time:08-03

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.

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.

CodePudding user response:

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.

  • Related