JRR
JRR

Reputation: 588

Python - re.sub return pattern rather than replacing

I am trying to revise a list of dictionary keys in Python 3 so that they are identifiable by the first set of numbers in the dictionary but it appears to be returning the regex pattern rather than the set of numbers.

>>>> import re
>>>>re.sub(r'GraphImages_[0-9]{2}_edge_media_to_caption_edges_0_node_text', '(?<=GraphImages_)\n{3}', 'GraphImages_99_edge_media_to_caption_edges_0_node_text')
'(?<=GraphImages_)\n{3}'
>>>>re.sub(r'GraphImages_[0-9]{2}_edge_media_to_caption_edges_0_node_text', '(?<=GraphImages_)\n{3}', 'GraphImages_123_edge_media_to_caption_edges_0_node_text')
'(?<=GraphImages_)\n{3}'

When the intended output from the above output would be 99 and 123 respectively.

Any guidance would be much appreciated. I am not very adept at the re package

Upvotes: 1

Views: 97

Answers (3)

DYZ
DYZ

Reputation: 57033

If you just want to extract the numbers, you need to find them, not to replace:

re.findall("GraphImages_([0-9]{2,})", yourstring)[0]
#'99'

In fact, in your case a split may be a better choice:

yourstring.split("_")[1]
#'99'

Upvotes: 1

JRR
JRR

Reputation: 588

Found a cumbersome workaround in the following

try_1 = re.sub('[^0-9]', "", 'GraphImages_99_edge_media_to_caption_edges_0_node_text')
try_2 = re.sub( '0$', "" , try_1)

Upvotes: 1

Jan
Jan

Reputation: 43169

You may use

^\D+(\d+).+

See a demo on regex101.com.

Upvotes: 0

Related Questions