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 8988143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:57:19+00:00 2026-06-15T21:57:19+00:00

I am trying to scan documents and identify where sections of the document begin

  • 0

I am trying to scan documents and identify where sections of the document begin and end. Sometimes, the document has a table of contents that lists page numbers I do not want to capture the TOC because it does not identify part of the document. I have been messing with this for sometime and am stuck on something. I can’t seem to avoid capturing the lines from the table of contents with line numbers

Here is the regular expression

verbose_item_pattern_3 = re.compile(r"""
  ^            # begin match at newline
  \t*          # 0-or-more tabspace
  [ ]*         # 0-or-more blank space
  I            # a capital I
  [tT][eE][mM] # one character from each of the three sets this allows for unknown case
  \t*          # 0-or-more tabspace
  [ ]*         # 0-or-more blankspace
  \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]
  $           # 1 newline character
  """, re.VERBOSE|re.MULTILINE)

here is an example of a line I DO NOT want to capture

test_string='\nItem 6.       TITLE ITEM 6..................................................25\n'

Here is an example of what I do want to capture

test_string='\nItem 6.       TITLE ITEM 6 maybe other words here who knows  \n'

But when I run

re.findall(verbose_item_pattern_3,test_string)

the result is

['Item 6.       TITLE ITEM 6..................................................25\n']

Now the thing to me that is interesting is that if my test string is this

test_string='PART I\nItem 1.       TITLE ITEM 1...................................................1\nItem 2.       TITLE ITEM 2..................................................21\n'

and run that with
re.findall(verbose_item_pattern_3,test_string)

the result is closer to what I want but still not correct

['Item 2.       TITLE ITEM 2..................................................21\n']

There should not be anything captured

  • 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-15T21:57:20+00:00Added an answer on June 15, 2026 at 9:57 pm

    Your regex matches because of three things.

    1. most of it is optional, so it is very unspecific
    2. there is a .* that eats the entire line, so your last condition [^0-9] will never come to bear, and that’s because:
    3. the newline character itself fulfills [^0-9], so the [^0-9] can successfully match even though the line ends in a number.

    The minimal change would be to use a negative look-behind at the end:

    verbose_item_pattern_3 = re.compile(r"""
      ^            # start-of-line
      \t*          # 0-or-more tabspace
      [ ]*         # 0-or-more blank space
      I            # a capital I
      [tT][eE][mM] # one character from each of the three sets this allows for unknown case
      \t*          # 0-or-more tabspace
      [ ]*         # 0-or-more blankspace
      \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
      $            # end-of-line
      (?<![0-9])   # NOT preceded by a decimal digit (via look-behind)
      """, re.VERBOSE|re.MULTILINE)
    

    Note that neither the ^ not the $ actually match a newline character. They match the position right after (^) or the position right before ($) a newline character. The newline character itself is never part of the match.

    I’ve changed their comments to the more precise start-of-line and end-of-line for that reason.

    Also note how I can apply a negative look-behind even after the $. Doing it this way is useful to prevent backtracking, making the regex faster.

    • 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 a scanning application. That application will scan the document
I am trying to scan my page on document load and convert all links
I am trying to make a Java tool that will scan the structure of
I am trying to create a php file that would scan a directory, make
I am trying to write a program that scan an ip range and detect
I'm trying to write a very simple terminal app that will scan for Bluetooth
I'm trying to write a SAX parser for an XHTML document that I download
I am trying to scan some documents to find dates for a classification problem.
i'm trying to Scan document and images in asp.net (C#) program ,please tell me
I have a table Document that is referenced by a ton of other tables

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.