467,077 Members | 1,035 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,077 developers. It's quick & easy.

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...

Jul 17 '05 #1
  • viewed: 1829
Share:
5 Replies
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.

JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #2
"Jan Pieter Kunst" <de*****@cauce.org> wrote in message
news:42***********************@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
Jul 17 '05 #3
Jan Pieter Kunst wrote:
: Because (as far as I understand it) backslashes have to be escaped
: twice: once for the regular expression engine, and once for PHP.
Matt Mitchell wrote:
One way to get round this is to use single quotes instead:

'/\\+$/' gets round the esacaping problem.


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
Jul 17 '05 #4
Thank you! All answers are helpful!

Jul 17 '05 #5
"John Dunlop" <us*********@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
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Derek Basch | last post: by
reply views Thread by Dave | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.