What is the proper regular expression for an unescaped backslash before a character?

前端 未结 4 877
刺人心
刺人心 2020-12-31 11:18

Let\'s say I want to represent \\q (or any other particular \"backslash-escaped character\"). That is, I want to match \\q but not \\\\q

4条回答
  •  悲哀的现实
    2020-12-31 12:02

    Updated: My new and improved Perl regex, supporting more than 3 backslashes:

    /(?\\\\)* # an even number of backslashes
      \\q       # Followed by a \q
      /x;

    or if your regex library doesn't support extended syntax.

    /(?\\\\)*\\q/

    Output of my test program:

    q does not match
    \q does match
    \\q does not match
    \\\q does match
    \\\\q does not match
    \\\\\q does match

    Older version

    /(?:(?
        

提交回复
热议问题