I just want to get "66664324", the content between ")" and "-".
Why did the search method get the ")" and "-" themselves.
a="(021)66664324-01"
b1=re.findall('\)(.*)-',a)
>['66664324']
b2=re.search('\)(.*)-',a).group()
>')66664324-'
What are differences between the two Code snippets.
Try printing the group(1) in re.search instead of group(). Where group() prints the whole match but group(1) prints only the captured group 1(printig chars which was present inside the group index 1).
>>> a="(021)66664324-01"
>>> import re
>>> b2=re.search('\)(.*)-',a).group(1)
>>> b2
'66664324'
>>> b2=re.search('\)(.*)-',a).group()
>>> b2
')66664324-'
But re.findall gives the first preference to groups rather than the match and also it returns the results in lists but search didn't. So that this b1=re.findall('\)(.*)-',a) gives you the desired output. If a group is present then re.findall method would print only the groups not the match. If no groups are present, then only it prints the match.
>>> b1=re.findall('\)(.*)-',a)
>>> b1
['66664324']
>>> b1=re.findall('\).*-',a)
>>> b1
[')66664324-']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With