By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,571 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,571 IT Pros & Developers. It's quick & easy.

help with regex

P: n/a
Hello,

I am trying to include the literal '\' (backslash) character in a regex.
I am not sure if I need to escape it, however...

From http://www.regular-expressions.info/charclass.html:
"To include a backslash as a character without any special meaning
inside a character class, you have to escape it with another backslash.
[\\x] matches a backslash or an x."

From http://www.tin.org/bin/man.cgi?section=7&topic=regex:
"...all other special characters, including `\', lose their special
significance within a bracket expression."

These seem to contradict each other. Both ways *seem* to work, but I am
not familiar enough with regex's to know if one is producing some other
side effect that I am not aware of. Thanks a bunch for your help.
Aug 3 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Marcus <Ju********@aol.comwrote:
I am trying to include the literal '\' (backslash) character in a regex.
I am not sure if I need to escape it, however...

From http://www.regular-expressions.info/charclass.html:
"To include a backslash as a character without any special meaning
inside a character class, you have to escape it with another backslash.
[\\x] matches a backslash or an x."

From http://www.tin.org/bin/man.cgi?section=7&topic=regex:
"...all other special characters, including `\', lose their special
significance within a bracket expression."

These seem to contradict each other. Both ways *seem* to work, but I am
not familiar enough with regex's to know if one is producing some other
side effect that I am not aware of. Thanks a bunch for your help.
I don't see the contradiction. What specifically do you find to be
contradictory there? Backslash loses its special significance inside
brackets, but you do need to escape it. Whenever you are referring to a
literal backslash in a regex, put \\ instead. Simple as that.

If you're still having problems, why not post your regex?

miguel
--
Photos from 40 countries on 5 continents: http://travel.u.nu
Latest photos: Malaysia; Thailand; Singapore; Spain; Morocco
Airports of the world: http://airport.u.nu
Aug 3 '06 #2

P: n/a
Miguel Cruz wrote:
Backslash loses its special significance inside
brackets, but you do need to escape it.
My confusion came from the fact that I was interpreting this statement
to mean that by losing its "special significance" (escape character), it
was just treated as a literal character when inside [].
Aug 3 '06 #3

P: n/a
Marcus:
I am trying to include the literal '\' (backslash) character in a regex.
>From the beginning. What planet are you on? What country? Town?
System? PHP version? Regex flavour?
I am not sure if I need to escape it, however...

From http://www.regular-expressions.info/charclass.html:
"To include a backslash as a character without any special meaning
inside a character class, you have to escape it with another backslash.
[\\x] matches a backslash or an x."

From http://www.tin.org/bin/man.cgi?section=7&topic=regex:
"...all other special characters, including `\', lose their special
significance within a bracket expression."
The first is talking about regexes in general, the second about POSIX
regexes.

Take it from the horse's mouth: 'All non-alphanumeric characters other
than \, -, ^ (at the start) and the terminating ] are non-special in
character classes, but it does no harm if they are escaped.'.

http://www.php.net/manual/en/referen...ern.syntax.php
These seem to contradict each other.
If they're talking about the same flavour of regex, yes.

--
Jock

Aug 4 '06 #4

P: n/a
John Dunlop wrote:
Marcus:
>I am trying to include the literal '\' (backslash) character in a regex.
>>From the beginning. What planet are you on? What country? Town?
System? PHP version? Regex flavour?
> I am not sure if I need to escape it, however...

From http://www.regular-expressions.info/charclass.html:
"To include a backslash as a character without any special meaning
inside a character class, you have to escape it with another backslash.
[\\x] matches a backslash or an x."

From http://www.tin.org/bin/man.cgi?section=7&topic=regex:
"...all other special characters, including `\', lose their special
significance within a bracket expression."

The first is talking about regexes in general, the second about POSIX
regexes.

Take it from the horse's mouth: 'All non-alphanumeric characters other
than \, -, ^ (at the start) and the terminating ] are non-special in
character classes, but it does no harm if they are escaped.'.

http://www.php.net/manual/en/referen...ern.syntax.php
>These seem to contradict each other.

If they're talking about the same flavour of regex, yes.
I am new to PHP but I just saw a free tool for building pcre patterns.
http://www.waterproof.fr/products/RegExpEditor
Thanks in Advance...
IchBin, Pocono Lake, Pa, USA http://weconsultants.phpnet.us
__________________________________________________ ________________________

'If there is one, Knowledge is the "Fountain of Youth"'
-William E. Taylor, Regular Guy (1952-)
Aug 4 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.