Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

why doesn't this fail?

Last post 01-11-2012, 8:47 AM by killahbeez. 3 replies.
Sort Posts: Previous Next
  •  01-09-2012, 7:38 AM 84474

    why doesn't this fail?

    I am using PCRE (same with .Net) and I would have expected the following pattern to fail when i run it against the text below:

    (?<B><)?[^%>]+(?(B)>)%

    that is: look for a < , look for at least 1 char which is not % or > then, if a < was found look for a >, then look for a %

    in other words find expressions like ....% and <.....>%, if a < is there then there MUST be  a >

    but when i run it on " <dsa z% d" it does not fail and finds the 1st 7 chars to match, what am i missing?

    thanks

    Filed under: ,
  •  01-11-2012, 3:15 AM 84481 in reply to 84474

    Re: why doesn't this fail?

    So, let's simplify it like this

    (<)?[^%>]++(?(1)>)%

    It's the same but I put posessive quantifier ++, instead of + 

    Apply this to

    <didi%

    The regexp engine do this:

    - first it tries for "<", will match

    - then will match "didi"

    - then, cause it find the first "<" will try to match ">", but it fails, so the engine backtrack again to the first "<", now because it's optional, the "<" will be matched by [^%>]+ part, so the conditional part (?(1)>) will not match (it will skip that part because first group, B in your case, isn't match after backtracking)

    - then will match %

     

     


    http://portal-vreme.ro
  •  01-11-2012, 8:30 AM 84482 in reply to 84481

    Re: why doesn't this fail?

    Of course, silly me. I should have used  "^ *(?<B><)?[^%><]+(?(B)>)%"

    Thanks

  •  01-11-2012, 8:47 AM 84483 in reply to 84482

    Re: why doesn't this fail?

    Use posessive quantifier [^%><]++ OR atomic grouping (?>[^%><]+) for that part, cause is more efficient that way in the case of non matching

     


    http://portal-vreme.ro
View as RSS news feed in XML