Reputation: 1015
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?
Upvotes: 0
Views: 110
Reputation: 27180
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.
Upvotes: 1