language agnostic - Regex not being greedy enough -


I've got the following regeses that were working perfectly until the new condition was created

  ^. * [? & Amp;] U (?: RL)? = (? & Lt; url; gt; *) $  

Actually, it is used against the URL, to catch everything after u =, or URL =

URL =

Unfortunately, a strange case came out

/ P>

Ideally, I need a URL "", instead, just edit it ""

Although it is not beautiful

  ^. * [? & Amp;] (? & Lt;! [? & Amp;] U (?: RL)? =. *) U (?: RL)? = (? & Lt; url & gt; *) $  

problem

The problem is not that . * is not greedy enough; It is that other . * which appears first also is greedy.

To illustrate this problem, example. Consider the following two samples; They are similar, except for the reluctance of \ 1 in the second rule:

  \ 1 greedy, \ 2 greedy \ 1 reluctance, \ 2 greedy ^ ([0 - 5] *) ([5- 9] *) $ ^ ([0-5] *?) ([5-9] *) $  

Here we have two capturing groups . \ 1 capture [0-5] * , and \ 2 capture [5-9] * . How these patterns match and capture it is a side-by-side comparison:

  \ 1 greedy, \ 2 grasping \ 1 reluctance, \ 2 greedy ^ ([0 -5] *) ([5- 9] *) $ ^ ([0-5] *?) ($ 5-9) *) $ Input group 1 group 2 group 1 group 2 54321098765 543210 98765 543210 98765 007 00 7 00 7 0123456789 012345 6789 01234 5678 95050 050 65050555 555 & lt; Empty & gt; & Lt; Empty & gt; 555 5550555 5550555 & lt; Empty & gt; 5550 555  

Note that as greedy as \ 2 , it can only hold that which is \ 1 before Not found before! Thus, if you want to make \ 2 as possible to 5 , you have to relieve \ 1 , therefore 5 is actually to grab from \ 2 .

Attachment

  • Related Questions


    OK

    So implementing it in your problem, there are two ways that you can fix it: You can see the first . * can make reluctant, so ():

      ^. *? [? & Amp;] U (?: RL)? = (? & Lt; url & gt; *) $  

    Alternatively, you can get rid of the prefix matching completely ():

      [? & Amp;] U (?: RL)? = (? & Lt; url & gt; *) $  

    Comments

    Popular posts from this blog

    windows - Heroku throws SQLITE3 Read only exception -

    lex - Building a lexical Analyzer in Java -

    python - rename keys in a dictionary -