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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:09:53+00:00 2026-06-03T13:09:53+00:00

So essentially I’m looking for specifically a 4 digit code within two angle brackets

  • 0

So essentially I’m looking for specifically a 4 digit code within two angle brackets within a text file. I know that I need to open the text file and then parse line by line, but I am not sure the best way to go about structuring my code after checking “for line in file”.

I think I can either somehow split it, strip it, or partition, but I also wrote a regex which I used compile on and so if that returns a match object I don’t think I can use that with those string based operations. Also I’m not sure whether my regex is greedy enough or not…

I’d like to store all instances of those found hits as strings within either a tuple or a list.

Here is my regex:

regex = re.compile("(<(\d{4,5})>)?")

I don’t think I need to include all that much code considering its fairly basic so far.

  • 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-03T13:09:55+00:00Added an answer on June 3, 2026 at 1:09 pm
    import re
    pattern = re.compile("<(\d{4,5})>")
    
    for i, line in enumerate(open('test.txt')):
        for match in re.finditer(pattern, line):
            print 'Found on line %s: %s' % (i+1, match.group())
    

    A couple of notes about the regex:

    • You don’t need the ? at the end and the outer (...) if you don’t want to match the number with the angle brackets, but only want the number itself
    • It matches either 4 or 5 digits between the angle brackets

    Update: It’s important to understand that the match and capture in a regex can be quite different. The regex in my snippet above matches the pattern with angle brackets, but I ask to capture only the internal number, without the angle brackets.

    More about regex in python can be found here :
    Regular Expression HOWTO

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

Sidebar

Related Questions

Essentially, what I'm looking for is a function that would allow me to do
Essentially I am looking for a PHP function or equation that will output the
Essentially, I need to know how to make a UIWebView erase everything when you
Essentially, my question is this: There are two ways that I have experienced setting
Essentially, I want to pull text within a div tag from a document on
Essentially, I found an old piece of LINQ C# code that counted the most
Essentially I'd like to know just how compatible are the iPhone and the iPod
Essentially, what I want to do (in DirectX) is to take two partially-transparent images
Essentially, I need to parse the response string of the CHAT CREATE command with
Essentially, after compiling utility jars w/ ANT, I'm having the problem that all the

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.