Exact match with regular expression

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

    Exact match with regular expression

    Hi,
    I'd like to use regular expressions to parse a string and accept only
    valid strings. What I mean is the possibility to check if the whole
    string matches the regex.

    So if I have:
    >>p = re.compile('a*b *')
    I can match this: 'aaaaaabbb'
    >>m = p.match('aaaaaa bbb')
    >>m.group()
    'aaaaaabbb'

    But I'd like to get None with this: 'aabDDDDb'
    Instead it matches the first part:
    >>m = p.match('aabDDD Db')
    >>m.group()
    'aab'

    I know this is the expected behaviour, but I'm sure it should be
    possible doing what I said.
    Are there other methods I don't know about in the re module?

    Thanks.
  • Rob Williscroft

    #2
    Re: Exact match with regular expression

    Mr.SpOOn wrote in news:mailman.30 69.1225039892.3 487.python-
    list@python.org in comp.lang.pytho n:
    Hi,
    I'd like to use regular expressions to parse a string and accept only
    valid strings. What I mean is the possibility to check if the whole
    string matches the regex.
    >
    So if I have:
    >
    >>>p = re.compile('a*b *')
    >
    I can match this: 'aaaaaabbb'
    >
    >>>m = p.match('aaaaaa bbb')
    >>>m.group()
    'aaaaaabbb'
    >
    But I'd like to get None with this: 'aabDDDDb'
    Instead it matches the first part:
    >
    >>>m = p.match('aabDDD Db')
    >>>m.group()
    'aab'
    Read (and bookmark) this:

    The official home of the Python Programming Language


    You want the 3rd item down about the "$" special character.
    >>p = re.compile('a*b *$')
    >>m = p.match('aabDDD Db')
    >>m is None
    True
    >>m = p.match('aaaaaa bbb')
    >>m.group()
    'aaaaaabbb'

    Rob.
    --

    Comment

    • Lawrence D'Oliveiro

      #3
      Re: Exact match with regular expression

      In message <Xns9B43B11B290 6ArtwfreenetREM OVEcouk@216.196 .109.145>, Rob
      Williscroft wrote:
      Funny how you never get a thank-you when you tell people to RTFM.

      Comment

      • Shawn Milochik

        #4
        Re: Exact match with regular expression

        On Fri, Oct 31, 2008 at 8:57 PM, Lawrence D'Oliveiro
        <ldo@geek-central.gen.new _zealandwrote:
        In message <Xns9B43B11B290 6ArtwfreenetREM OVEcouk@216.196 .109.145>, Rob
        Williscroft wrote:
        >>
        Funny how you never get a thank-you when you tell people to RTFM.
        --

        >

        Hmmm. Kind of odd. Someone involved in the language they're having
        trouble with wrote some documentation for the exact thing they're
        asking for, you tell them where it is, and that doesn't deserve
        thanks?

        Something's wrong here. Well, it's about time for that which must be
        read by everyone on the Internet to be mentioned again.

        If you haven't read this yet, read this:


        Especially if you have asked for help and not gotten what you wanted.
        Especially if people ignore your questions.
        Especially if people give answers that don't help you.

        Comment

        • Rob Williscroft

          #5
          Re: Exact match with regular expression

          Lawrence D'Oliveiro wrote in news:geg9i4$51b $2@lust.ihug.co .nz in
          comp.lang.pytho n:
          In message <Xns9B43B11B290 6ArtwfreenetREM OVEcouk@216.196 .109.145>, Rob
          Williscroft wrote:
          >>
          Funny how you never get a thank-you when you tell people to RTFM.
          Saying "Thank You" is what email is for, which in this case is
          what Mr.SpOOn did:
          Read (and bookmark) this:

          The official home of the Python Programming Language


          You want the 3rd item down about the "$" special character.
          >
          Great, thanks.
          When I read about the symbol in the tutorial I didn't realize
          what was it for.
          Rob.
          --

          Comment

          • Mr.SpOOn

            #6
            Re: Exact match with regular expression

            On Sat, Nov 1, 2008 at 1:57 AM, Lawrence D'Oliveiro
            <ldo@geek-central.gen.new _zealandwrote:
            In message <Xns9B43B11B290 6ArtwfreenetREM OVEcouk@216.196 .109.145>, Rob
            Williscroft wrote:
            >>
            Funny how you never get a thank-you when you tell people to RTFM.
            My fault :\
            I said "thank you" to Rob, but I just sent a private message. It's
            just that I did a reply and not reply to all.

            Anyway, I RTFM and solved my problem :)

            Thanks again,
            Bye

            Comment

            • Lawrence D'Oliveiro

              #7
              Re: Exact match with regular expression

              In message <mailman.3423.1 225717725.3487. python-list@python.org >, Mr.SpOOn
              wrote:
              On Sat, Nov 1, 2008 at 1:57 AM, Lawrence D'Oliveiro
              <ldo@geek-central.gen.new _zealandwrote:
              >
              >Funny how you never get a thank-you when you tell people to RTFM.
              >
              My fault :\
              I said "thank you" to Rob, but I just sent a private message. It's
              just that I did a reply and not reply to all.
              That's OK. My fault for not realizing. :)

              Comment

              Working...