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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:11:18+00:00 2026-05-18T08:11:18+00:00

Code: str = ‘<br><br />A<br />B’ print(re.sub(r'<br.*?>\w$’, ”, str)) It is expected to return

  • 0

Code:

str = '<br><br />A<br />B'
print(re.sub(r'<br.*?>\w$', '', str))

It is expected to return <br><br />A, but it returns an empty string ''!

Any suggestion?

  • 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-05-18T08:11:19+00:00Added an answer on May 18, 2026 at 8:11 am

    Greediness works from left to right, but not otherwise. It basically means “don’t match unless you failed to match”. Here’s what’s going on:

    1. The regex engine matches <br at the start of the string.
    2. .*? is ignored for now, it is lazy.
    3. Try to match >, and succeeds.
    4. Try to match \w and fails. Now it’s interesting – the engine starts backtracking, and sees the .*? rule. In this case, . can match the first >, so there’s still hope for that match.
    5. This keep happening until the regex reaches the slash. Then >\w can match, but $ fails. Again, the engine comes back to the lazy .* rule, and keeps matching, until it matches<br><br />A<br />B

    Luckily, there’s an easy solution: By replacing <br[^>]*>\w$ you don’t allow matching outside of your tags, so it should replace the last occurrence.
    Strictly speaking, this doesn’t work well for HTML, because tag attributes can contain > characters, but I assume it’s just an example.

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

Sidebar

Related Questions

Here is the code: string str; cin>>str; cout<<first input:<<str<<endl; getline(cin, str); cout<<line input:<<str<<endl; The
Suppose I have the following code: foreach(string str in someObj.GetMyStrings()) { // do some
The following code receives seg fault on line 2: char *str = string; str[0]
Here is a sample code: public class TestIO{ public static void main(String[] str){ TestIO
I have this code: var str = ' abc'; str.replace( ,); but it is
i have a problem in string manipulation here is the code string str =
I have this code: string Str = <String><MoreString>; int Start = Str.LastIndexOf('<') + 1;
Is there any function which replaces params in a string? Something like this: Code:
I have this code Dim str As String Dim myConn As SqlConnection = New
Language: asp This is sample of my code: str = www.example.com/gotobuy.aspx?id=1234 key_word = .obuy.

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.