regular expression questions in Python

Discussion in 'Python' started by Jeremy, Jul 11, 2005.

  1. Jeremy

    Jeremy Guest

    I am (very) new top regular expressions and I am having a difficult time
    understanding how to do them. I have the following in my script:

    zaidsearch = r'''^ {5,}([\d]{4,5})(.\d{2,2}c)'''
    ZAIDSearch = re.compile(search, re.IGNORECASE)

    When I do: ZAID.search(...) then this works fine. I would like to write
    it as:

    zaidsearch = r'''^ {5,}([\d]{4,5}) #My comments
    (.\d{2,2}c) #More of my comments'''
    ZAIDSearch = re.compile(zaidsearch, re.VERBOSE)

    but this doesn't work. I get the following error:

    raise error, v # invalid expression
    sre_constants.error: nothing to repeat


    So I guess my question is: how do I use the VERBOSE option to make my
    regular expression easier to understand for a human? Secondly, how can
    I use both the VERBOSE and IGNORECASE options?
    Thanks,
    Jeremy
     
    Jeremy, Jul 11, 2005
    #1
    1. Advertising

  2. Jeremy wrote:
    > I am (very) new top regular expressions and I am having a difficult time
    > understanding how to do them. I have the following in my script:
    >
    > zaidsearch = r'''^ {5,}([\d]{4,5})(.\d{2,2}c)'''
    > ZAIDSearch = re.compile(search, re.IGNORECASE)
    >
    > When I do: ZAID.search(...) then this works fine. I would like to write
    > it as:
    >
    > zaidsearch = r'''^ {5,}([\d]{4,5}) #My comments

    ^ This space will not be a space in the RE.
    You have to specify a space with '[ ]' or '\ '
    when using re.VERBOSE

    > (.\d{2,2}c) #More of my comments'''
    > ZAIDSearch = re.compile(zaidsearch, re.VERBOSE)
    >
    > but this doesn't work. I get the following error:
    >
    > raise error, v # invalid expression
    > sre_constants.error: nothing to repeat



    Reinhold
     
    Reinhold Birkenfeld, Jul 11, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VSK
    Replies:
    2
    Views:
    2,353
  2. =?iso-8859-1?B?bW9vcJk=?=

    Matching abitrary expression in a regular expression

    =?iso-8859-1?B?bW9vcJk=?=, Dec 1, 2005, in forum: Java
    Replies:
    8
    Views:
    872
    Alan Moore
    Dec 2, 2005
  3. GIMME
    Replies:
    3
    Views:
    12,024
    vforvikash
    Dec 29, 2008
  4. bbxrider

    newbie regular expression questions

    bbxrider, Nov 22, 2003, in forum: Perl Misc
    Replies:
    11
    Views:
    164
    Gunnar Hjalmarsson
    Nov 24, 2003
  5. Dang Griffith

    Two regular expression questions

    Dang Griffith, Feb 2, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    87
    Dang Griffith
    Feb 3, 2004
Loading...

Share This Page