Bug in re.findall?

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

    Bug in re.findall?

    Hello everyone,

    Is there a bug in re.findall in Python 2.4? See:

    subnetlist="192 .168.100.0 , 192.168.101.0"
    ipre=re.compile ("([0-9]{1,3}\.){3}[0-9]{1,3}")
    >>ipre.findall( subnetlist)
    ['100.', '101.']


    But:

    a=ipre.finditer (subnetlist)
    >>a.next().grou p()
    '192.168.100.0'
    >>a.next().grou p()
    '192.168.101.0'
    >>a.next().grou p()
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    StopIteration

    Also:
    >>ipre.search(s ubnetlist).grou p()
    '192.168.100.0'

    Is this a bug or am I doing smth wrong?

  • dwahli@gmail.com

    #2
    Re: Bug in re.findall?

    On Jul 4, 12:33 pm, Marcin Krol <mrk...@gmail.c omwrote:
    Hello everyone,
    >
    Is there a bug in re.findall in Python 2.4? See:
    >
    subnetlist="192 .168.100.0 , 192.168.101.0"
    ipre=re.compile ("([0-9]{1,3}\.){3}[0-9]{1,3}")
    >
     >>ipre.findall( subnetlist)
    >
    ['100.', '101.']
    >
    But:
    >
    a=ipre.finditer (subnetlist)
    >
     >>a.next().grou p()
    '192.168.100.0'
     >>a.next().grou p()
    '192.168.101.0'
     >>a.next().grou p()
    Traceback (most recent call last):
       File "<stdin>", line 1, in ?
    StopIteration
    >
    Also:
    >
     >>ipre.search(s ubnetlist).grou p()
    '192.168.100.0'
    >
    Is this a bug or am I doing smth wrong?
    Look strange but match the Python documentation for re.findall:
    "If one or more groups are present in the pattern, return a list of
    groups; this will be a list of tuples if the pattern has more than one
    group"

    you must use this RE for your example to match what you expect:
    ipre=re.compile ("(?:[0-9]{1,3}\.){3}[0-9]{1,3}")

    but using re.finditer is IMHO better.

    Cheer,
    Dom

    Comment

    • Peter Otten

      #3
      Re: Bug in re.findall?

      Marcin Krol wrote:
      Hello everyone,
      >
      Is there a bug in re.findall in Python 2.4? See:
      >
      subnetlist="192 .168.100.0 , 192.168.101.0"
      ipre=re.compile ("([0-9]{1,3}\.){3}[0-9]{1,3}")
      >
      >>ipre.findall( subnetlist)
      >
      ['100.', '101.']
      >
      >
      But:
      >
      a=ipre.finditer (subnetlist)
      >
      >>a.next().grou p()
      '192.168.100.0'
      >>a.next().grou p()
      '192.168.101.0'
      >>a.next().grou p()
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      StopIteration
      >
      Also:
      >
      >>ipre.search(s ubnetlist).grou p()
      '192.168.100.0'
      >
      Is this a bug or am I doing smth wrong?
      From the doc:

      """
      findall( pattern, string[, flags])
      Return a list of all non-overlapping matches of pattern in string. If one
      or more groups are present in the pattern, return a list of groups; this
      will be a list of tuples if the pattern has more than one group.
      """

      So findall()'s behaviour changes depending on the number of explicit groups

      None:

      [m.group() for m in re.finditer(... )]

      One:

      [m.group(1) for m in re.finditer(... )]

      More than one:

      [m.groups() for m in re.finditer(... )]

      all in accordance with the documentation.

      Peter

      Comment

      Working...