re.search

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

    #1

    re.search

    Hi,

    Isn't the following code supposed to return ('1994')?
    [color=blue][color=green][color=darkred]
    >>> re.search('(\d{ 4})?', '4 1994').groups()[/color][/color][/color]
    (None,)

    Thanks,
    Ray
  • James Stroud

    #2
    Re: re.search

    Rares Vernica wrote:[color=blue]
    > Hi,
    >
    > Isn't the following code supposed to return ('1994')?
    >[color=green][color=darkred]
    > >>> re.search('(\d{ 4})?', '4 1994').groups()[/color][/color]
    > (None,)
    >
    > Thanks,
    > Ray[/color]

    The ? is allowing it to not match before it finds the 1994.

    Note:

    py> re.search('(\d{ 4})?', '1994 4').groups()
    ('1994',)

    James

    --
    James Stroud
    UCLA-DOE Institute for Genomics and Proteomics
    Box 951570
    Los Angeles, CA 90095


    Comment

    • Fredrik Lundh

      #3
      Re: re.search

      Rares Vernica wrote:
      [color=blue]
      > Isn't the following code supposed to return ('1994')?
      >[color=green][color=darkred]
      > >>> re.search('(\d{ 4})?', '4 1994').groups()[/color][/color]
      > (None,)[/color]

      it's supposed to return the first thing that matches your pattern, which,
      in this case, is the empty string at the beginning of the target string.

      if you want to look for groups of four digits, remove the "?"

      </F>



      Comment

      Working...