Regular Expression - "\" chars at the end of the string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sbd!

    Regular Expression - "\" chars at the end of the string

    I had a problem with string which have some \ chars at the end of the
    string. For example:

    $String = "I want this **** out of here\\\\\\\\\";

    I needed to remove it useing regular expression...i thinked that
    something like "/\\+$/" is enough but i was wrong. I made some tries
    and finaly i removed "\" chars it with "/\\\+$/". It works (i'm proud
    with myself :)), but i can't understand why \\\ matchs single \, and \\
    not...

  • Jan Pieter Kunst

    #2
    Re: Regular Expression - "\&quot ; chars at the end of the string

    sbd! wrote:[color=blue]
    > I had a problem with string which have some \ chars at the end of the
    > string. For example:
    >
    > $String = "I want this **** out of here\\\\\\\\\";
    >
    > I needed to remove it useing regular expression...i thinked that
    > something like "/\\+$/" is enough but i was wrong. I made some tries
    > and finaly i removed "\" chars it with "/\\\+$/". It works (i'm proud
    > with myself :)), but i can't understand why \\\ matchs single \, and \\
    > not...
    >[/color]

    Because (as far as I understand it) backslashes have to be escaped
    twice: once for the regular expression engine, and once for PHP.

    JP

    --
    Sorry, <devnull@cauce. org> is a spam trap.
    Real e-mail address unavailable. 5000+ spams per month.

    Comment

    • Matt Mitchell

      #3
      Re: Regular Expression - &quot;\&quot ; chars at the end of the string

      "Jan Pieter Kunst" <devnull@cauce. org> wrote in message
      news:4217aa7c$0 $28980$e4fe514c @news.xs4all.nl ...
      : sbd! wrote:
      : > I had a problem with string which have some \ chars at the end of the
      : > string. For example:
      : >
      : > $String = "I want this **** out of here\\\\\\\\\";
      : >
      : > I needed to remove it useing regular expression...i thinked that
      : > something like "/\\+$/" is enough but i was wrong. I made some tries
      : > and finaly i removed "\" chars it with "/\\\+$/". It works (i'm proud
      : > with myself :)), but i can't understand why \\\ matchs single \, and \\
      : > not...
      : >
      :
      : Because (as far as I understand it) backslashes have to be escaped
      : twice: once for the regular expression engine, and once for PHP.

      One way to get round this is to use single quotes instead:

      '/\\+$/' gets round the esacaping problem. Don't double-quote PREG strings,
      it gives you no end of trouble!

      If you have to interpolate variable values into perl regexes, have a look at


      Matt


      Comment

      • John Dunlop

        #4
        Re: Regular Expression - &quot;\&quot ; chars at the end of the string

        Jan Pieter Kunst wrote:
        [color=blue]
        > : Because (as far as I understand it) backslashes have to be escaped
        > : twice: once for the regular expression engine, and once for PHP.[/color]

        Matt Mitchell wrote:
        [color=blue]
        > One way to get round this is to use single quotes instead:
        >
        > '/\\+$/' gets round the esacaping problem.[/color]

        No, you still need to escape the backslash twice.

        That pattern matches the same as "/\\+$/"; that is, a plus
        sign at the end of the subject string or before a newline at
        the end. Only one backslash is passed to the regular
        expression engine, so the plus sign no longer carries its
        special meaning as a quantifier.



        --
        Jock

        Comment

        • sbd!

          #5
          Re: Regular Expression - &quot;\&quot ; chars at the end of the string

          Thank you! All answers are helpful!

          Comment

          • Matt Mitchell

            #6
            Re: Regular Expression - &quot;\&quot ; chars at the end of the string

            "John Dunlop" <usenet+2004@jo hn.dunlop.name> wrote in message : >
            : > '/\\+$/' gets round the esacaping problem.
            :
            : No, you still need to escape the backslash twice.
            :

            Whoops, not thinking before posting again...

            <g>
            Matt


            Comment

            Working...