siamii
siamii

Reputation: 24094

Return only group in regex find all

I have a long text and want to match all occurances of "the rate is (\d+\.\d)%" but want to only return the group (\d+\.d) as a list of matched strings. How can I do that?

I can't just match the group, because it occurs in other context as well.

Example

"I like how the rate is 6.7%. Now the rate is 11.4% profits were down by 5.6%"

In this case I would need

[6.7, 11.4]

I see, I thought that findall return the whole string matched, not the group. Thank you for the clarification.

Upvotes: 0

Views: 2916

Answers (3)

TAS
TAS

Reputation: 2079

This can be done using re.findall().

See 7.2. re — Regular expression operations

Upvotes: -1

avasal
avasal

Reputation: 14854

In [94]: s="I like how the rate is 6.7%. Now the rate is 11.4% profits were down
 by 5.6%"

In [95]: re.findall(r'the rate is (\d+\.\d)%', s)
Out[95]: ['6.7', '11.4']

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1121834

Sure you can, just group the part you want returned:

r'the rate is (\d+\.d)%'

so provide enough context to only match what you want returned, and use a capturing group. Then use the .findall() method, which will only include the matched capturing groups:

>>> re.findall(r'the rate is (\d+\.\d)%', "I like how the rate is 6.7%. Now the rate is 11.4% profits were down by 5.6%")
['6.7', '11.4']

Upvotes: 1

Related Questions