Connecting Tech Pros Worldwide Help | Site Map

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

sbd!
Guest
 
Posts: n/a
#1: Jul 17 '05
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
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Regular Expression - "\" 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.
Matt Mitchell
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Regular Expression - "\" 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
http://php.net/preg_quote

Matt


John Dunlop
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Regular Expression - "\" 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.

http://www.php.net/manual/en/language.types.string.php

--
Jock
sbd!
Guest
 
Posts: n/a
#5: Jul 17 '05

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


Thank you! All answers are helpful!

Matt Mitchell
Guest
 
Posts: n/a
#6: Jul 17 '05

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


"John Dunlop" <usenet+2004@john.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


Closed Thread