You need the first captured group:
a.group(1)
b.group(1)
...
without any captured group specification as argument to group(), it will show the full match, like what you're getting now.
Here's an example:
In [8]: string_one = 'file_record_transcript.pdf'
In [9]: re.search(r'^(file.*)\.pdf$', string_one).group()
Out[9]: 'file_record_transcript.pdf'
In [10]: re.search(r'^(file.*)\.pdf$', string_one).group(1)
Out[10]: 'file_record_transcript'
Answer from heemayl on Stack OverflowYou need the first captured group:
a.group(1)
b.group(1)
...
without any captured group specification as argument to group(), it will show the full match, like what you're getting now.
Here's an example:
In [8]: string_one = 'file_record_transcript.pdf'
In [9]: re.search(r'^(file.*)\.pdf$', string_one).group()
Out[9]: 'file_record_transcript.pdf'
In [10]: re.search(r'^(file.*)\.pdf$', string_one).group(1)
Out[10]: 'file_record_transcript'
you can also use match[index]
a[0] => Full match (file_record_transcript.pdf)
a[1] => First group (file_record_transcript)
a[2] => Second group (if any)
Regex: Capture multiple matches of a single group
Regex capture / non-capture groups best practice
regex capture groups
Accessing a "symbolic group name" in Python regex
Videos
I'm frustrated trying to do something that I think should be easy. Any help is greatly appreciated!
I want to find all the instances of a string that match my regex and capture part of the match in a group. In perl it's an easy one-liner:
#stores the contents of every <td> element in the array @results my @results = $inputString =~ m/<td>(.*?)<\/td>/g;
My simplified example could be accomplished using various html-parsing libraries, but my actual parsing job can't. So far the only Python solution I can find is to use re.findall and then individually parse out my group from the whole match. Is there a better way to do this in Python?