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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:42:23+00:00 2026-06-04T11:42:23+00:00

str = <test>0</test> print re.search(<.*?>, str).group() print re.search(>.*?<, str).group() >> <text> >> >0< How

  • 0
str = "<test>0</test>"
print re.search("<.*?>", str).group()
print re.search(">.*?<", str).group()
>> <text>
>> >0<

How can I get it so that the resulting text is “test” and “0” and not include the two characters I used as markers in the regex?

  • 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-04T11:42:24+00:00Added an answer on June 4, 2026 at 11:42 am

    You shouldn’t be using regex to parse XML/HTML, see murgatroid99’s comment.

    That being said, here is how you can get the results you want for this example using regex. Use a capturing group:

    >>> s = "<test>0</test>"
    >>> print re.search(r"<(.*?)>", s).group(1)
    test
    >>> print re.search(r">(.*?)<", s).group(1)
    0
    

    Note that you shouldn’t use str as a variable name, as it will mask the built-in type.

    An alternative to a capturing group would be a lookbehind and lookahead:

    >>> print re.search(r"(?<=<).*?(?=>)", s).group()
    test
    >>> print re.search(r"(?<=>).*?(?=<)", s).group()
    0
    

    Using raw string literals (r"...") isn’t necessary for these in particular, but it is good to get into the habit of using them when writing regular expressions to make sure that backslashes are handled properly.

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

Sidebar

Related Questions

I have an xml file that contains its element like <ab:test>Str</ab:test> When I am
I came across a nice one-liner to search for text and print out the
I have the following: rule = http://www.abc.com/ test = http://www.abc.com/test print(str(re.compile(rule).match(test))) I want this
I want to new a StringArray as follow: class Test { public: QString* str[];
str = "&&"; label1.Text = str; but it only shows "&". How do I
string str = one three; string::iterator it; string add = two ; Lets say
def myFunc( str ): print str=, str if str == None: print str is
def group_move(group, damper): # Make a copy to test values new = group #
I am using a simple live search script that displays the results from a
I have two python dictionaries containing information about japanese words and characters: vocabDic :

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.