idiot question about preg_match and regex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lkrubner@geocities.com

    idiot question about preg_match and regex

    I apologize for my igorance of Regex. I can't get this function to
    work. It gives the following error message.



    function checkForFloatin gPoint($input=f alse) {
    $pattern = "[-+]?([0-9]*\.[0-9]+|[0-9]+)";
    $match = preg_match (pattern, subject);
    echo "The match is: $match ";
    return $match;
    }




    Warning: preg_match(): Delimiter must not be alphanumeric or backslash
    in /home/hoovera/public_html/lawrenceScripts/lk_lib.php on line 642
    The match is:

  • lkrubner@geocities.com

    #2
    Re: idiot question about preg_match and regex


    I know this is bad technique, but if I want to say "any 2 digits
    followed by a decimal followed by any 5 more digits" then I could say
    this, yes?

    [0-9][0-9]\.[0-9][0-9][0-9][0-9][0-9]

    Comment

    • Michael Winter

      #3
      Re: idiot question about preg_match and regex

      On 27/10/2005 14:28, lkrubner@geocit ies.com wrote:

      [snip]
      [color=blue]
      > function checkForFloatin gPoint($input=f alse) {[/color]

      I don't really see how boolean false is a useful default value, or even
      why you'd want a default argument for the input.

      What is this function supposed to do? Exactly what pattern(s) are you
      trying to match or test for? It's identifier suggests that it should
      look for some aspect of a floating-point number (though it's not clear
      what), yet your pattern allows integers as well.
      [color=blue]
      > $pattern = "[-+]?([0-9]*\.[0-9]+|[0-9]+)";[/color]

      You haven't included any delimiters. A forward slash, at both the start
      and end of the pattern, is common.
      [color=blue]
      > $match = preg_match (pattern, subject);[/color]

      You haven't included the dollar ($) symbol before each identifier.

      [snip]

      Mike


      Don't use tabs to indent. Tabs are usually rendered with the equivalent
      of eight spaces, which is far too much when content is usually wrapped
      at eighty characters, or less. Use spaces; preferably two.

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

      Comment

      • Meião

        #4
        Re: idiot question about preg_match and regex

        just change
        $pattern "/[-+]?([0-9]*\.[0-9]+|[0-9]+)/";

        see the slashes?

        Comment

        • Meião

          #5
          Re: idiot question about preg_match and regex

          [0-9]{2}\.[0-9]{5}
          looks better

          Comment

          • lkrubner@geocities.com

            #6
            Re: idiot question about preg_match and regex

            >What is this function supposed to do? Exactly what pattern(s) are you[color=blue]
            >trying to match or test for? It's identifier suggests that it should
            >look for some aspect of a floating-point number (though it's not clear
            >what), yet your pattern allows integers as well.[/color]

            Thank you for help. I apologize for asking such a poor question. I've a
            simple one to follow up with: how do you say "not" in regex? Is it "^"?

            I've decided that all I want to do is make sure there are no letters in
            the input. Is it right to say this:

            $pattern "[^A-Za-z]";
            $match = preg_match ($pattern, $subject);

            This comes back true if there are no letters in $subject, yes?

            Comment

            • Ewoud Dronkert

              #7
              Re: idiot question about preg_match and regex

              lkrubner@geocit ies.com wrote:[color=blue]
              > how do you say "not" in regex? Is it "^"?[/color]

              Yes and no. http://php.net/manual/en/reference.p...ern.syntax.php
              [color=blue]
              > I've decided that all I want to do is make sure there are no letters in
              > the input. Is it right to say this:
              >
              > $pattern "[^A-Za-z]";[/color]

              That is not a pattern because it doesn't have pattern delimiters. It also
              doesn't have any quantifiers (+, * or {}), or start- or end-of-subject
              pinning (^ and $). And you can use a pattern modifier for case-insensitive
              matching, see


              --
              E. Dronkert

              Comment

              Working...