preg_match not working

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

    preg_match not working

    Apologies if this has been asked before - I can't find anything on Google or
    Google Groups.

    I am running PHP 5.0.4 on Apache 2.0.54 with the PCRE extension installed.
    For some reason though, the preg_match function is recognised by PHP but it
    won't seem to accept any regexes as arguments.

    For example:

    $result = preg_match("[0-9][0-9]","12")

    gives me an error of: Unknown modifier '['

    and

    $result = preg_match("hel lo","hello")

    gives me an error of: Delimiter must not be alphanumeric or backslash

    Am I doing something wrong here?

    Thanks in advance

    Andrew Richardson
  • p

    #2
    Re: preg_match not working

    [color=blue]
    > $result = preg_match("[0-9][0-9]","12")
    >
    > gives me an error of: Unknown modifier '['
    >
    > and
    >
    > $result = preg_match("hel lo","hello")
    >
    > gives me an error of: Delimiter must not be alphanumeric or backslash[/color]

    You still need to wrap the regex in slashes I believe:

    $result = preg_match("/[0-9][0-9]/","12")
    $result = preg_match("/hello/","hello")

    Hope that helps,
    Pete

    Comment

    • Andrew Richardson

      #3
      Re: preg_match not working

      p wrote:
      [color=blue]
      > You still need to wrap the regex in slashes I believe:
      >
      > $result = preg_match("/[0-9][0-9]/","12")
      > $result = preg_match("/hello/","hello")
      >
      > Hope that helps,
      > Pete[/color]

      Ah, that would be it. I feel a bit silly now! :)

      Thanks!

      Comment

      • Michael Winter

        #4
        Re: preg_match not working

        On 06/03/2006 22:56, Andrew Richardson wrote:
        [color=blue]
        > $result = preg_match("[0-9][0-9]","12")
        >
        > gives me an error of: Unknown modifier '['
        >
        > and
        >
        > $result = preg_match("hel lo","hello")
        >
        > gives me an error of: Delimiter must not be alphanumeric or backslash
        >
        > Am I doing something wrong here?[/color]

        You aren't including delimiters. The first character in a pattern will
        be considered to be a pattern delimiter. A closing delimiter marks the
        end of the pattern and the start of the flags.

        In the first example above, the opening bracket is treated as the
        delimiter and the next unescaped closing bracket will end the pattern.

        In the second example, there are no delimiters at all.

        There are various characters that can be used. Braces and brackets
        (round, square, or angle), are considered in pairs. That is, the opening
        bracket marks the start of the pattern, and the closing bracket ends it.
        Any other non-alphanumeric or backslash character is used twice; the
        same character starts and ends the pattern.

        A common delimiter is the forward slash:

        $result = preg_match('/[0-9][0-9]/', '12');

        Hope that helps,
        Mike

        --
        Michael Winter
        Prefix subject with [News] before replying by e-mail.

        Comment

        • Andrew Richardson

          #5
          Re: preg_match not working

          Michael Winter wrote:
          [color=blue]
          > On 06/03/2006 22:56, Andrew Richardson wrote:[/color]

          <snip>
          [color=blue][color=green]
          >> Am I doing something wrong here?[/color]
          >
          > You aren't including delimiters. The first character in a pattern will
          > be considered to be a pattern delimiter. A closing delimiter marks the
          > end of the pattern and the start of the flags.[/color]

          <snip>
          [color=blue]
          > Hope that helps,
          > Mike[/color]

          Thanks Mike, I'd done regexes using preg_match before and forgot about the
          delimiters. Now all I have to do is get my regex to work. :)

          Comment

          • Toby Inkster

            #6
            Re: preg_match not working

            Andrew Richardson wrote:
            [color=blue]
            > $result = preg_match("[0-9][0-9]","12")
            >
            > gives me an error of: Unknown modifier '['[/color]

            $result = preg_match("/[0-9][0-9]/","12");

            --
            Toby A Inkster BSc (Hons) ARCS
            Contact Me ~ http://tobyinkster.co.uk/contact

            Comment

            Working...