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

The Archive Base Latest Questions

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

I have a string, where text='<tr align=right><td>12</td><td>John</td> and I would like to extract the

  • 0

I have a string, where

text='<tr align="right"><td>12</td><td>John</td> 

and I would like to extract the tuple (’12’, ‘John’). It is working fine when I am using

m=re.findall(r'align.{13}(\d+).*([A-Z]\w+).*([A-Z]\w+)', text)

print m

but I am getting (‘2’, ‘John’), when I am using

m=re.findall(r'align.+(\d+).*([A-Z]\w+).*([A-Z]\w+)', text)
print m

Why is it going wrong? I mean why .{13} works fine, but .+ fails to work in my re?
Thank you!

  • 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-15T18:28:20+00:00Added an answer on June 15, 2026 at 6:28 pm

    I can’t actually test this with the sample text and regexps you provided, because as written they clearly should find no matches, and in fact do find no matches in both 2.7 and 3.3.

    But I’m guessing that you want a non-greedy match, and changing .+ to .+? will fix whatever your problem is.

    As Jon Clements points out in his answer, you really shouldn’t be using regular expressions here. Regexps cannot actually parse non-regular languages like XML. Of course, despite what the purists say, regexps can still be a useful hack for non-regular languages in quick&dirty cases. But as soon as you run into something that isn’t working, the first think you ought to do is consider that maybe this isn’t one of those quick&dirty cases, and you should look for a real parser. Even if you’d never used the ElementTree API before, or XPath, they’re pretty easy to learn, and the time spent learning is definitely not wasted, as it will come in handy many times in the future.

    But anyway… let’s reduce your sample to something that works as you describe, and see what this does:

    >>> text='<tr align="right"><td>12</td><td>John</td> 
    SyntaxError: EOL while scanning string literal
    >>> text='<tr align="right"><td>12</td><td>John</td>'
    >>> re.findall(r'align.{13}(\d+).*([A-Z]\w+).*([A-Z]\w+)', text)
    []
    >>> re.findall(r'align.{13}(\d+).*([A-Z]\w+)', text)
    [('12', 'John')]
    >>> re.findall(r'align.+(\d+).*([A-Z]\w+).*([A-Z]\w+)', text)
    []
    >>> re.findall(r'align.+(\d+).*([A-Z]\w+)', text)
    [('2', 'John')]
    

    I think this is what you were complaining about. Well, .+ is not “not working properly”; it’s doing exactly what you asked it to: match at least one character, and as many as possible, up to the point where the rest of the expression still has something to match. Which includes matching the 1, because the rest of the expression still matches.

    If you want it to instead stop matching as soon as the rest of the expression can take over, that’s a non-greedy match, not a greedy match, so you want +? rather than +. Let’s try it:

    >>> re.findall(r'align.+?(\d+).*([A-Z]\w+)', text)
    [('12', 'John')]
    

    Tada.

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

Sidebar

Related Questions

I have string: text and something and something like f213 @@ -1,9 +1,11 @@
I have a string like: hello #this# is #some text string# text text I
I have a string of text that contains html, and I need to extract
I have a string of text like so: var foo = FooBar; I want
Suppose I have a string of text, of all characters Latin-based. With punctuation. How
Suppose you have a string with text in two or more scripts. When you
Suppose I have this code: String encoding = UTF-16; String text = [Hello StackOverflow];
So, I have a string of text: string with a bunch of links but
I have a text string that has been doctored to be web safe URLs
I have a text string with HTML embedded in it. It also has CRs

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.