help with regex

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

    #1

    help with regex

    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.
  • Miguel Cruz

    #2
    Re: help with regex

    Marcus <JumpMan222@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

    Comment

    • Marcus

      #3
      Re: help with regex

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

      Comment

      • John Dunlop

        #4
        Re: help with regex

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


        These seem to contradict each other.
        If they're talking about the same flavour of regex, yes.

        --
        Jock

        Comment

        • IchBin

          #5
          Re: help with regex

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

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



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

          Comment

          Working...