0

I am trying to parse through various strings that look like this

cpd00015_c + cpd00041_c + cpd00095_c --> 2.0 cpd00001_c + cpd00009_c + cpd00067_c

Right now I have code that finds the first instance, but I want to find all the instances of these.

try:
found = re.search('cpd(.+?)_c', reactionEquation).group(1)
print(found)
except AttributeError:
# AAA, ZZZ not found in the original string
pass # apply your error handling

the re.search function only finds the first instance of this. Is there a research for multiple strings that you don't fully know the name of?

2 Answers2

0

To get all matches use re.findall

Ex:

import re
s = "cpd00015_c + cpd00041_c + cpd00095_c --> 2.0 cpd00001_c + cpd00009_c + cpd00067_c"
print( re.findall("cpd(.+?)_c", s) )

Output:

['00015', '00041', '00095', '00001', '00009', '00067']
Rakesh
  • 81,458
  • 17
  • 76
  • 113
0

Just need to update search to find all and remove group.

import re
try:
    found_all = re.findall('cpd(.+?)_c', reactionEquation)
    for found in found_all : 
        print(found)
except AttributeError:
    # AAA, ZZZ not found in the original string
    pass # apply your error handling
Venkata Gogu
  • 1,021
  • 10
  • 25