Unwanted regex does not select closest choice

My regex does not select the closest "cont" pair to the inner text. How can i fix this?

Input:

cont cont ItextI /cont /cont 

Regex:

 cont.*?I(.*?)I.*?/cont 

Match:

 cont cont ItextI /cont 

Compliance I need:

 cont ItextI /cont 
+4
source share
2 answers
 cont(?:(?!/?cont).)*I(.*?)I(?:(?!/?cont).)*/cont 

will only match the indoor unit itself.

Explanation:

 cont # match "cont" (?: # Match... (?!/?cont) # (as long as we're not at the start of "cont" or "/cont") . # any character. )* # Repeat any number of times. I # Match "I" (.*?) # Match as few characters as possible, capturing them. I # Match "I" (?: # Same as above (?!/?cont) . )* /cont # Match "/cont" 

This explicitly prevents cont or /cont appearing between opening cont and the text to be captured (and between that text and closing /cont ).

+12
source

The reason you map to cont cont ItextI /cont is because the regex matches the first part of your cont pattern in the first "cont", then uses reluctant .*? to gobble up the space, the next space and the space preceding ItextI . When it reaches ItextI , it recognizes I as matching the next part of the pattern and continues the rest of the regular expression. As minitech writes, this is because the regular expression works from the beginning of the line and finds the earliest possible match.

If you can make assumptions about a space, you can write:

 cont\s+I(.*?)I\s+/cont 

This will match your example above.

+2
source

Source: https://habr.com/ru/post/1394877/


All Articles