regular expression - matches

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

    #1

    regular expression - matches

    how can i determine if a given character sequence matches my regex,
    completely?

    in java for example I can do,
    Pattern.compile (regex).matcher (input).matches ()

    this returns True/False whether or not input matches the regex
    completely.

    is there a matches in python?

  • Tim Chase

    #2
    Re: regular expression - matches

    abcd wrote:
    how can i determine if a given character sequence matches my regex,
    completely?
    >
    in java for example I can do,
    Pattern.compile (regex).matcher (input).matches ()
    >
    this returns True/False whether or not input matches the regex
    completely.
    >
    is there a matches in python?
    >>import re
    >>'match' in dir(re)
    True
    >>help(re.match )
    Help on function match in module sre:

    match(pattern, string, flags=0)
    Try to apply the pattern at the start of the string, returning
    a match object, or None if no match was found.


    For more info, see

    Source code: Lib/re/ This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings ( str) as well as 8-...


    -tkc





    Comment

    • abcd

      #3
      Re: regular expression - matches

      yea i saw that....guess I was trusting that my regex was accurate :)
      ....b/c i was getting a Matcher when I shouldnt have, but i found that
      it must be the regex.

      Comment

      • Simon Forman

        #4
        Re: regular expression - matches

        abcd wrote:
        how can i determine if a given character sequence matches my regex,
        completely?
        >
        in java for example I can do,
        Pattern.compile (regex).matcher (input).matches ()
        >
        this returns True/False whether or not input matches the regex
        completely.
        >
        is there a matches in python?
        Yes. It's called match and it's in the re module
        (http://docs.python.org/lib/module-re.html)

        Python's re.match() matches from the start of the string, so if you
        want to ensure that the whole string matches completely you'll probably
        want to end your re pattern with the "$" character (depending on what
        the rest of your pattern matches.)

        HTH,
        ~Simon

        Comment

        • James Oakley

          #5
          Re: regular expression - matches

          On Friday 21 July 2006 10:57 am, abcd wrote:
          yea i saw that....guess I was trusting that my regex was accurate :)
          ...b/c i was getting a Matcher when I shouldnt have, but i found that
          it must be the regex.
          regular expressions, python regular expressions, debugging regular expressions, developing regular expressions, develop regular expressions, debug regular expressions, python regex, python re, python gui regex, python gui regular expressions


          Makes regex generation and debugging much easier.

          --
          James Oakley
          jfunk@funktroni cs.ca

          Comment

          • John Salerno

            #6
            Re: regular expression - matches

            Simon Forman wrote:
            Python's re.match() matches from the start of the string, so if you
            want to ensure that the whole string matches completely you'll probably
            want to end your re pattern with the "$" character (depending on what
            the rest of your pattern matches.)
            Is that necessary? I was thinking that match() was used to match the
            full RE and string, and if they weren't the same, they wouldn't match
            (meaning a begin/end of string character wasn't necessary). That's wrong?

            Comment

            • Steve Holden

              #7
              Re: regular expression - matches

              John Salerno wrote:
              Simon Forman wrote:
              >
              >
              >>Python's re.match() matches from the start of the string, so if you
              >>want to ensure that the whole string matches completely you'll probably
              >>want to end your re pattern with the "$" character (depending on what
              >>the rest of your pattern matches.)
              >
              >
              Is that necessary? I was thinking that match() was used to match the
              full RE and string, and if they weren't the same, they wouldn't match
              (meaning a begin/end of string character wasn't necessary). That's wrong?
              That's wrong. In this context match just means you got to the end of the
              pattern. However, if you don't want to add the "$" to the end of the
              patterns, you could instead check that

              m.endpos == len(s)

              where m is the match object and s is the subject string.

              regards
              Steve
              --
              Steve Holden +44 150 684 7255 +1 800 494 3119
              Holden Web LLC/Ltd http://www.holdenweb.com
              Skype: holdenweb http://holdenweb.blogspot.com
              Recent Ramblings http://del.icio.us/steve.holden

              Comment

              • Simon Forman

                #8
                Re: regular expression - matches

                John Salerno wrote:
                Simon Forman wrote:
                >
                Python's re.match() matches from the start of the string, so if you
                want to ensure that the whole string matches completely you'll probably
                want to end your re pattern with the "$" character (depending on what
                the rest of your pattern matches.)
                >
                Is that necessary? I was thinking that match() was used to match the
                full RE and string, and if they weren't the same, they wouldn't match
                (meaning a begin/end of string character wasn't necessary). That's wrong?
                My understanding, from the docs and from dim memories of using
                re.match() long ago, is that it will match on less than the full input
                string if the re pattern allows it (for instance, if the pattern
                *doesn't* end in '.*' or something similar.)

                I'd test this, though, before trusting it.

                What the heck, I'll do that now:
                >>import re
                >>re.match('ab' , 'abcde')
                <_sre.SRE_Mat ch object at 0xb6ff8790>
                >>m = _
                >>m.group()
                'ab'
                >>print re.match('ab$', 'abcde')
                None


                Yup! That's the case.

                Peace,
                ~Simon

                Comment

                • John Salerno

                  #9
                  Re: regular expression - matches

                  Simon Forman wrote:
                  John Salerno wrote:
                  >Simon Forman wrote:
                  >>
                  >>Python's re.match() matches from the start of the string, so if you
                  >>want to ensure that the whole string matches completely you'll probably
                  >>want to end your re pattern with the "$" character (depending on what
                  >>the rest of your pattern matches.)
                  >Is that necessary? I was thinking that match() was used to match the
                  >full RE and string, and if they weren't the same, they wouldn't match
                  >(meaning a begin/end of string character wasn't necessary). That's wrong?
                  >
                  My understanding, from the docs and from dim memories of using
                  re.match() long ago, is that it will match on less than the full input
                  string if the re pattern allows it (for instance, if the pattern
                  *doesn't* end in '.*' or something similar.)
                  >
                  I'd test this, though, before trusting it.
                  >
                  What the heck, I'll do that now:
                  >
                  >>>import re
                  >>>re.match('ab ', 'abcde')
                  <_sre.SRE_Mat ch object at 0xb6ff8790>
                  >>>m = _
                  >>>m.group()
                  'ab'
                  >>>print re.match('ab$', 'abcde')
                  None
                  >
                  >
                  Yup! That's the case.
                  >
                  Peace,
                  ~Simon
                  >
                  Thanks guys!

                  Comment

                  • Simon Forman

                    #10
                    Re: regular expression - matches


                    John Salerno wrote:
                    Thanks guys!
                    A pleasure. : )

                    Comment

                    • John Machin

                      #11
                      Re: regular expression - matches

                      On 22/07/2006 9:25 AM, John Machin wrote:

                      Apologies if this appears twice ... post to the newsgroup hasn't shown
                      up; trying the mailing-list.
                      On 22/07/2006 2:18 AM, Simon Forman wrote:
                      >John Salerno wrote:
                      >>Simon Forman wrote:
                      >>>
                      >>>Python's re.match() matches from the start of the string, so if you
                      >
                      (1) Every regex library's match() starts matching from the beginning of
                      the string (unless of course there's an arg for an explicit starting
                      position) -- where else would it start?
                      >
                      (2) This has absolutely zero relevance to the "match whole string or
                      not" question.
                      >
                      >>>want to ensure that the whole string matches completely you'll probably
                      >>>want to end your re pattern with the "$" character (depending on what
                      >>>the rest of your pattern matches.)
                      >
                      *NO* ... if you want to ensure that the whole string matches completely,
                      you need to end your pattern with "\Z", *not* "$".
                      >
                      Perusal of the manual would seem to be indicated :-)
                      >
                      >>Is that necessary? I was thinking that match() was used to match the
                      >>full RE and string, and if they weren't the same, they wouldn't match
                      >>(meaning a begin/end of string character wasn't necessary). That's
                      >>wrong?
                      >
                      Yes. If the default were to match the whole string, then a metacharacter
                      would be required to signal "*don't* match the whole string" ...
                      functionality which is quite useful.
                      >
                      >>
                      >My understanding, from the docs and from dim memories of using
                      >re.match() long ago, is that it will match on less than the full input
                      >string if the re pattern allows it (for instance, if the pattern
                      >*doesn't* end in '.*' or something similar.)
                      >
                      Ending a pattern with '.*' or something similar is typically a mistake
                      and does nothing but waste CPU cycles:
                      >
                      C:\junk>python -mtimeit -s"import
                      re;s='a'+80*'z' ;m=re.compile(' a').match" "m(s)"
                      1000000 loops, best of 3: 1.12 usec per loop
                      >
                      C:\junk>python -mtimeit -s"import
                      re;s='a'+8000*' z';m=re.compile ('a').match" "m(s)"
                      100000 loops, best of 3: 1.15 usec per loop
                      >
                      C:\junk>python -mtimeit -s"import
                      re;s='a'+80*'z' ;m=re.compile(' a.*').match" "m(s)"
                      100000 loops, best of 3: 1.39 usec per loop
                      >
                      C:\junk>python -mtimeit -s"import
                      re;s='a'+8000*' z';m=re.compile ('a.*').match" "m(s)"
                      10000 loops, best of 3: 24.2 usec per loop
                      >
                      The regex engine can't optimise it away because '.' means by default
                      "any character except a newline" , so it has to trundle all the way to
                      the end just in case there's a newline lurking somewhere.
                      >
                      Oh and just in case you were wondering:
                      >
                      C:\junk>python -mtimeit -s"import
                      re;s='a'+8000*' z';m=re.compile ('a.*',re.DOTAL L).match" "m(s)"
                      1000000 loops, best of 3: 1.18 usec per loop
                      >
                      In this case, logic says the '.*' will match anything, so it can stop
                      immediately.
                      >
                      >>
                      >I'd test this, though, before trusting it.
                      >>
                      >What the heck, I'll do that now:
                      >>
                      >>>>import re
                      >>>>re.match('a b', 'abcde')
                      ><_sre.SRE_Matc h object at 0xb6ff8790>
                      >>>>m = _
                      >
                      ??? What's wrong with _.group() ???
                      >
                      >>>>m.group()
                      >'ab'
                      >>>>print re.match('ab$', 'abcde')
                      >None
                      >>
                      >
                      HTH,
                      John
                      >
                      >
                      >

                      Comment

                      • John Machin

                        #12
                        Re: regular expression - matches

                        On 22/07/2006 2:18 AM, Simon Forman wrote:
                        John Salerno wrote:
                        >Simon Forman wrote:
                        >>
                        >>Python's re.match() matches from the start of the string, so if you
                        (1) Every regex library's match() starts matching from the beginning of
                        the string (unless of course there's an arg for an explicit starting
                        position) -- where else would it start?

                        (2) This has absolutely zero relevance to the "match whole string or
                        not" question.
                        >>want to ensure that the whole string matches completely you'll probably
                        >>want to end your re pattern with the "$" character (depending on what
                        >>the rest of your pattern matches.)
                        *NO* ... if you want to ensure that the whole string matches completely,
                        you need to end your pattern with "\Z", *not* "$".

                        Perusal of the manual would seem to be indicated :-)
                        >Is that necessary? I was thinking that match() was used to match the
                        >full RE and string, and if they weren't the same, they wouldn't match
                        >(meaning a begin/end of string character wasn't necessary). That's wrong?
                        Yes. If the default were to match the whole string, then a metacharacter
                        would be required to signal "*don't* match the whole string" ...
                        functionality which is quite useful.
                        >
                        My understanding, from the docs and from dim memories of using
                        re.match() long ago, is that it will match on less than the full input
                        string if the re pattern allows it (for instance, if the pattern
                        *doesn't* end in '.*' or something similar.)
                        Ending a pattern with '.*' or something similar is typically a mistake
                        and does nothing but waste CPU cycles:

                        C:\junk>python -mtimeit -s"import
                        re;s='a'+80*'z' ;m=re.compile(' a').match" "m(s)"
                        1000000 loops, best of 3: 1.12 usec per loop

                        C:\junk>python -mtimeit -s"import
                        re;s='a'+8000*' z';m=re.compile ('a').match" "m(s)"
                        100000 loops, best of 3: 1.15 usec per loop

                        C:\junk>python -mtimeit -s"import
                        re;s='a'+80*'z' ;m=re.compile(' a.*').match" "m(s)"
                        100000 loops, best of 3: 1.39 usec per loop

                        C:\junk>python -mtimeit -s"import
                        re;s='a'+8000*' z';m=re.compile ('a.*').match" "m(s)"
                        10000 loops, best of 3: 24.2 usec per loop

                        The regex engine can't optimise it away because '.' means by default
                        "any character except a newline" , so it has to trundle all the way to
                        the end just in case there's a newline lurking somewhere.

                        Oh and just in case you were wondering:

                        C:\junk>python -mtimeit -s"import
                        re;s='a'+8000*' z';m=re.compile ('a.*',re.DOTAL L).match" "m(s)"
                        1000000 loops, best of 3: 1.18 usec per loop

                        In this case, logic says the '.*' will match anything, so it can stop
                        immediately.
                        >
                        I'd test this, though, before trusting it.
                        >
                        What the heck, I'll do that now:
                        >
                        >>>import re
                        >>>re.match('ab ', 'abcde')
                        <_sre.SRE_Mat ch object at 0xb6ff8790>
                        >>>m = _
                        ??? What's wrong with _.group() ???
                        >>>m.group()
                        'ab'
                        >>>print re.match('ab$', 'abcde')
                        None
                        >
                        HTH,
                        John

                        Comment

                        Working...