Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005485
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:09:26+00:00 2026-06-16T01:09:26+00:00

I am trying to use a verbose regular expression in Python (2.7). If it

  • 0

I am trying to use a verbose regular expression in Python (2.7). If it matters I am just trying to make it easier to go back and more clearly understand the expression sometime in the future. Because I am new I first created a compact expression to make sure I was getting what I wanted.

Here is the compact expression:

test_verbose_item_pattern = re.compile('\n{1}\b?I[tT][eE][mM]\s+\d{1,2}\.?\(?[a-e]?\)?.*[^0-9]\n{1}')

It works as expected

Here is the Verbose expression

verbose_item_pattern = re.compile("""
\n{1}       #begin with a new line allow only one new line character
\b?       #allow for a word boundary the ? allows 0 or 1 word boundaries \nITEM or \n  ITEM
I        # the first word on the line must begin with a capital I
[tT][eE][mM]  #then we need one character from each of the three sets this allows for unknown case
\s+       # one or more white spaces this does allow for another \n not sure if I should change it
\d{1,2}    # require one or two digits
\.?        # there could be 0 or 1 periods after the digits 1. or 1
\(?        # there might be 0 or 1 instance of an open paren
[a-e]?      # there could be 0 or 1 instance of a letter in the range a-e
\)?         # there could be 0 or 1 instance of a closing paren
.*          #any number of unknown characters so we can have words and punctuation
[^0-9]     # by its placement I am hoping that I am stating that I do not want to allow strings that end with a number and then \n
\n{1}     #I want to cut it off at the next newline character
""",re.VERBOSE)

The problem is that when I run the verbose pattern I get an exception

Traceback (most recent call last):
File "C:/Users/Dropbox/directEDGAR-Code-Examples/NewItemIdentifier.py", line 17, in <module>
 """,re.VERBOSE)
 File "C:\Python27\lib\re.py", line 190, in compile
  return _compile(pattern, flags)
 File "C:\Python27\lib\re.py", line 242, in _compile
 raise error, v # invalid expression
 error: nothing to repeat

I am afraid this is going to be something silly but I can’t figure it out. I did take my verbose expressions and compact it line by line to make sure the compact version was the same as the verbose.

The error message states there is nothing to repeat?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:09:27+00:00Added an answer on June 16, 2026 at 1:09 am
    • It is a good habit to use raw string literals when defining regex
      patterns. A lot of regex patterns use backslashes, and using a raw
      string literal will allow you to write single backslashes instead of
      having to worry about whether or not Python will interpret your
      backslash to have a different meaning (and having to use two backslashes in those cases).

    • \b? is not valid regex. This is saying 0-or-1 word boundaries. But either you have a word boundary or you don’t. If you have a word boundary, then you have 1 word boundary. If you don’t have a word boundary then you have 0 word boundaries. So \b? would (if it were valid regex) be always true.

    • Regex makes a distinction between the end of a string and the end of a line. (A string may consist of multiple lines.)

      • \A matches only the start of a string.
      • \Z matches only the end of a string.
      • $ matches the end of a string, and also end of a line in re.MULTILINE mode.
      • ^ matches the start of a string, and also start of a line in re.MULTILINE mode.

    import re
    verbose_item_pattern = re.compile(r"""
        $            # end of line boundary
        \s{1,2}      # 1-or-2 whitespace character, including the newline
        I            # a capital I
        [tT][eE][mM] # one character from each of the three sets this allows for unknown case
        \s+          # 1-or-more whitespaces INCLUDING newline
        \d{1,2}      # 1-or-2 digits
        [.]?         # 0-or-1 literal .
        \(?          # 0-or-1 literal open paren
        [a-e]?       # 0-or-1 letter in the range a-e
        \)?          # 0-or-1 closing paren
        .*           # any number of unknown characters so we can have words and punctuation
        [^0-9]       # anything but [0-9]
        $            # end of line boundary
        """, re.VERBOSE|re.MULTILINE)
    
    x = verbose_item_pattern.search("""
     Item 1.0(a) foo bar
    """)
    
    print(x)
    

    yields

    <_sre.SRE_Match object at 0xb76dd020>
    

    (indicating there is a match)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to make use of .NET configuration and understand custom sections, elements
I'm trying to use a radio button in my modelform but its just outputting
I've been trying to use docopt to make a simple CLI, but for some
i'm trying use facebook API to upload photo in my fan page. I downloaded
I am trying use gem tire to search in my application. I have tables
I was trying use a set of filter functions to run the appropriate routine,
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
I am trying use a Java Uploader in a ROR app (for its ease
Hi I'm trying use a datepicker on a field I have. I'm trying to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.