regex

Why regex searching second group from the end?


I have a search in app and use regex to highlight found matches. But I notice sometimes it match kinda incorrectly (?).

For example, the regex (case insensitive): (the).*(te). And result for test phrase will be (tested here):

Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & JavaScript flavors of RegEx are supported. Validate your expression with Tests mode.

If you take a look at test phrase, it contain a lot of te parts before the last one. Feels like it searching from the end of the line. But I expect it should be like this:

Edit the Expression & Text to see matches. Roll over matches or the expression for details. PCRE & JavaScript flavors of RegEx are supported. Validate your expression with Tests mode.

I need it to find the first occurrence. Since in app it's user entered query, I don't know exactly what the regex will be. But it has a pattern: (word1).*(word2).*(word3).

What am I doing wrong? The regex is not correct or it's just a way of it's working?


Solution

  • Change the greedy quantifier .* to the reluctant quantifier .*?:

    the.*?te
    

    Notice also that you don't need the grouping brackets.

    See live demo.