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

PHP: matching an even amount only

Last post 05-16-2008, 2:54 PM by Kyosys. 6 replies.
Sort Posts: Previous Next
  •  05-16-2008, 12:44 PM 42342

    PHP: matching an even amount only

    A Problem that is probably rather simple, but I still fail to do this

    I want to match an even amount of backslashes (0,2,4,6,8,etc. but not 1,3,5,etc.) in front of a single quote ' only.
    So
    \' or \\\' wouldn't be matched
    but \\' or \\\\\\' would

    what I tried is
    (.([^\\]{2})*)'
    but that doesn't even remotely work
  •  05-16-2008, 12:53 PM 42343 in reply to 42342

    Re: PHP: matching an even amount only

    (?<=\\)(\\\\)+\x27

    worked for me vs your input

    \' or \\\' wouldn't be matched
    but \\' or \\\\\\' would

    got 2 matches

    Match$1
    \\'\\
    \\\\'\\

  •  05-16-2008, 1:36 PM 42345 in reply to 42343

    Re: PHP: matching an even amount only

    That doesn't work because it's not supposed to match it at all, if it's not an even amount (with yours, if it isn't it just matches one backslash less)

    but thanks anyway
  •  05-16-2008, 1:44 PM 42346 in reply to 42345

    Re: PHP: matching an even amount only

    we're obviously swimming in different logical pools, man, like my boss says ;=)

    my regex does not match on an odd number of backslashes in the input u sent, test it. Negative look-behind takes care of that. Or u have another input layout in mind?

  •  05-16-2008, 1:47 PM 42347 in reply to 42346

    Re: PHP: matching an even amount only

    u can test it really quick in

    http://regexlib.com/RETester.aspx

  •  05-16-2008, 1:55 PM 42348 in reply to 42347

    Re: PHP: matching an even amount only

    hey, it was my bad: i meant to send u this one: used positive look-behind instead of negative.

    (?<!\\)(\\\\)+\x27

    meaning : *match on an even # of backslashes NOT preceded by a backslash*

    so apparently my pool was different ;=)

     

  •  05-16-2008, 2:54 PM 42349 in reply to 42348

    Re: PHP: matching an even amount only

    yes, that one works, thanks a bunch!
View as RSS news feed in XML