regular expression help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • www.douglassdavis.com

    regular expression help

    I am using the preg_match function (in PHP) that uses perl regular
    expressions. Apparently I don't really understand regular expressions
    though. Could some one explain this?

    If this is the regular expression

    /^\s*(\d+\.\d+)| (\.\d+)|(\d+)\s *$/

    How does this:

    40:26:46.302N 79:56:55.903W

    match? I thought when I added the ^ and $ that meant it had to match
    the whole thing? It seems to only be matching .302

  • Tim Hammerquist

    #2
    Re: regular expression help

    ["Followup-To:" header set to comp.lang.perl. misc.]

    www.douglassdavis.com <doug@douglassd avis.com> wrote:[color=blue]
    > I am using the preg_match function (in PHP) that uses perl
    > regular expressions. Apparently I don't really understand
    > regular expressions though. Could some one explain this?[/color]

    GAH! Interesting way to rationalize a cross-post, but let's see
    if we can sort this out... :)
    [color=blue]
    > If this is the regular expression
    >
    > /^\s*(\d+\.\d+)| (\.\d+)|(\d+)\s *$/
    >
    > How does this:
    >
    > 40:26:46.302N 79:56:55.903W
    >
    > match? I thought when I added the ^ and $ that meant it had
    > to match the whole thing? It seems to only be matching .302[/color]

    It's the placement of your alternators ('|') that's messing you
    up. Because the |'s aren't inside any parens, the regex is
    split up into the following three sub-expressions and tried
    sequentially:

    /^\s*(\d+\.\d+)/ # doesn't allow for the ':'
    /(\.\d+)/ # matches because the anchors are excluded
    /(\d+)\s*$/ # never gets a chance to match, but
    # wouldn't anyway because it doesn't
    # allow for the 'W'

    You're probably looking for something more like:

    /^\s*(\d+\.\d+|\ .\d+|\d+)\s*$/

    or

    /^\s*(?:(\d+\.\d +)|(\.\d+)|(\d+ ))\s*$/

    depending on what you want to do.

    However, since you never actually stated what you *want* to
    match, I have no way of knowing what regex you need. I can only
    point out the probably cause of confusion. I.e., put your |'s
    inside a set of parentheses. :)

    HTH,
    Tim Hammerquist

    Comment

    • Anno Siegel

      #3
      Re: regular expression help

      www.douglassdavis.com <doug@douglassd avis.com> wrote in comp.lang.perl. misc:[color=blue]
      > I am using the preg_match function (in PHP) that uses perl regular
      > expressions. Apparently I don't really understand regular expressions
      > though. Could some one explain this?
      >
      > If this is the regular expression
      >
      > /^\s*(\d+\.\d+)| (\.\d+)|(\d+)\s *$/
      >
      > How does this:
      >
      > 40:26:46.302N 79:56:55.903W
      >
      > match? I thought when I added the ^ and $ that meant it had to match
      > the whole thing? It seems to only be matching .302[/color]

      Concatenation binds tighter than alternation. So the first alternative
      is anchored to the left margin and the last one is anchored to the right
      margin. The middle one isn't anchored and matched what it can.

      Anno
      --
      If you want to post a followup via groups.google.c om, don't use
      the broken "Reply" link at the bottom of the article. Click on
      "show options" at the top of the article, then click on the
      "Reply" at the bottom of the article headers.

      Comment

      • www.douglassdavis.com

        #4
        Re: regular expression help


        Anno Siegel wrote:[color=blue]
        > www.douglassdavis.com <doug@douglassd avis.com> wrote in comp.lang.perl. misc:[color=green]
        > > I am using the preg_match function (in PHP) that uses perl regular
        > > expressions. Apparently I don't really understand regular expressions
        > > though. Could some one explain this?
        > >
        > > If this is the regular expression
        > >
        > > /^\s*(\d+\.\d+)| (\.\d+)|(\d+)\s *$/
        > >
        > > How does this:
        > >
        > > 40:26:46.302N 79:56:55.903W
        > >
        > > match? I thought when I added the ^ and $ that meant it had to match
        > > the whole thing? It seems to only be matching .302[/color]
        >
        > Concatenation binds tighter than alternation. So the first alternative
        > is anchored to the left margin and the last one is anchored to the right
        > margin. The middle one isn't anchored and matched what it can.
        >[/color]

        That makes sense. Thanks. This seems to work:

        /^\s*((\d+\.\d+) |(\.\d+)|(\d+)) \s*$/

        Comment

        • Anno Siegel

          #5
          Re: regular expression help

          www.douglassdavis.com <doug@douglassd avis.com> wrote in comp.lang.perl. misc:[color=blue]
          >
          > Anno Siegel wrote:[color=green]
          > > www.douglassdavis.com <doug@douglassd avis.com> wrote in comp.lang.perl. misc:[color=darkred]
          > > > I am using the preg_match function (in PHP) that uses perl regular
          > > > expressions. Apparently I don't really understand regular expressions
          > > > though. Could some one explain this?
          > > >
          > > > If this is the regular expression
          > > >
          > > > /^\s*(\d+\.\d+)| (\.\d+)|(\d+)\s *$/
          > > >
          > > > How does this:
          > > >
          > > > 40:26:46.302N 79:56:55.903W
          > > >
          > > > match? I thought when I added the ^ and $ that meant it had to match
          > > > the whole thing? It seems to only be matching .302[/color]
          > >
          > > Concatenation binds tighter than alternation. So the first alternative
          > > is anchored to the left margin and the last one is anchored to the right
          > > margin. The middle one isn't anchored and matched what it can.
          > >[/color]
          >
          > That makes sense. Thanks. This seems to work:
          >
          > /^\s*((\d+\.\d+) |(\.\d+)|(\d+)) \s*$/[/color]

          Yes, but it messes up the capturing parentheses.

          /^\s*(?:(\d+\.\d +)|(\.\d+)|(\d+ ))\s*$/

          Anno
          --
          If you want to post a followup via groups.google.c om, don't use
          the broken "Reply" link at the bottom of the article. Click on
          "show options" at the top of the article, then click on the
          "Reply" at the bottom of the article headers.

          Comment

          • phal

            #6
            Re: regular expression help

            ---------------------------------------
            If this is the regular expression
            /^\s*(\d+\.\d+)| (\.\d+)|(\d+)\s *$/

            How does this:
            40:26:46.302N 79:56:55.903W

            ==========
            You can use this method
            =~/(\d{2}):(\d{2}) :(\d{2})\.(\d{4 })\s+(\d{2}):(\ d{2}):(\d{2})\. (\d{4})/gi

            I don't know whether you want to slice the letter or not, but you still
            able to retireve it by this method.
            I haven't check whehter it is correctly run, i just type in as what i
            see.

            Phal

            Comment

            • Marcin Dobrucki

              #7
              Re: regular expression help

              phal wrote:[color=blue]
              > ==========
              > You can use this method
              > =~/(\d{2}):(\d{2}) :(\d{2})\.(\d{4 })\s+(\d{2}):(\ d{2}):(\d{2})\. (\d{4})/gi[/color]

              Or you can sacrificie a few lines of code and use split:

              list ($latitude, $longitude) = split (" ", "40:26:46.3 02N
              79:56:55.903W") ;

              And then parse for ':' and for '.', or preg match. Note you can also
              do preg_split where you can dump your working regexp to create a list of
              values to be used within another script.

              Anybody motivated to write PEAR::GeoData?

              Comment

              • Anno Siegel

                #8
                Re: regular expression help

                phal <betterdie@gmai l.com> wrote in comp.lang.perl. misc:

                Please post attributions and some context when you reply.
                [color=blue]
                > ---------------------------------------
                > If this is the regular expression
                > /^\s*(\d+\.\d+)| (\.\d+)|(\d+)\s *$/
                >
                > How does this:
                > 40:26:46.302N 79:56:55.903W
                >
                > ==========
                > You can use this method
                > =~/(\d{2}):(\d{2}) :(\d{2})\.(\d{4 })\s+(\d{2}):(\ d{2}):(\d{2})\. (\d{4})/gi[/color]

                Why the "/i"? It does nothing.

                The OP doesn't want a regex that matches "40:26:46.3 02N 79:56:55.903W".
                He was wondering why his original regex did match it and wanted it
                corrected so that it doesn't.
                [color=blue]
                > I don't know whether you want to slice the letter or not, but you still[/color]

                Slice the letter?
                [color=blue]
                > able to retireve it by this method.
                > I haven't check whehter it is correctly run, i just type in as what i
                > see.[/color]

                Well, it doesn't match. Your regex requires \d{4}, twice. There is
                no sequence of four digits in the given string.

                All in all, a pretty useless contribution.

                Anno
                --
                If you want to post a followup via groups.google.c om, don't use
                the broken "Reply" link at the bottom of the article. Click on
                "show options" at the top of the article, then click on the
                "Reply" at the bottom of the article headers.

                Comment

                • www.douglassdavis.com

                  #9
                  Re: regular expression help


                  Marcin Dobrucki wrote:
                  [color=blue]
                  >
                  > Anybody motivated to write PEAR::GeoData?[/color]

                  Actually, I may as well... Already have it started. Expect to see it
                  soon.



                  --


                  Comment

                  • jim

                    #10
                    Re: regular expression help

                    I recommend regexplorer.


                    www.douglassdavis.com wrote:
                    [color=blue]
                    > I am using the preg_match function (in PHP) that uses perl regular
                    > expressions. Apparently I don't really understand regular expressions
                    > though. Could some one explain this?
                    >
                    > If this is the regular expression
                    >
                    > /^\s*(\d+\.\d+)| (\.\d+)|(\d+)\s *$/
                    >
                    > How does this:
                    >
                    > 40:26:46.302N 79:56:55.903W
                    >
                    > match? I thought when I added the ^ and $ that meant it had to match
                    > the whole thing? It seems to only be matching .302[/color]

                    Comment

                    Working...