0

My string is this:

<tr id="xyz21" style="" class="standard">

When I run my regex through the online regex helper site, pythex.org, I get what I'm wanting; only the number "21". The site says:

Match captures "21"

Here's the regex I used:

<tr id="xyz(.*?)"

However, when I use this same regex in my Python 3 script, I get much more. Here's the script with the result:

>>> import re
>>> x = '<tr id="xyz21" style="" class="standard">'
>>> num = re.search('<tr id="xyz(.*?)"', x).group()
>>> print(num)
<tr id="xyz21"

Ultimately, all I want is to create a variable with a value of "21". By the way, the actual string I use the regex on is a lot longer than what I'm showing. It's a small file, actually. I've simplified my example just to make it easier to understand. Any ideas?

1 Answer 1

1

You need to add a parameter:

re.search('<tr id="xyz(.*?)"', x).group(1)

The documentation noted that

If there is a single argument, the result is a single string; if there are multiple arguments, the result is a tuple with one item per argument. Without arguments, group1 defaults to zero (the whole match is returned). If a groupN argument is zero, the corresponding return value is the entire matching string; if it is in the inclusive range [1..99], it is the string matching the corresponding parenthesized group.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.