Capture groups with Regular [removed]Python)

后端 未结 1 1188
自闭症患者
自闭症患者 2020-12-15 16:30

Kind of a noob here, apologies if I misstep.

I\'m learning regular expressions and am on this lesson: https://regexone.com/lesson/capturing_groups.

In the

相关标签:
1条回答
  • 2020-12-15 17:23

    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'
    
    0 讨论(0)
提交回复
热议问题