lundi 2 mars 2015

Extract only the portion of a string between two regex patterns

I've looked at this question and although the answer solves the OP's problem it doesn't actually answer the question. How can I extract a section of a given string when only the pattern before and after the relevant section are known?


For example if I had a string like this:


"Hi, this is a string where only values = { 000.10, 2.00, 5.0, 10.0 } are important


And given this string I need to extract specifically 000.10, 2.00, 5.0, 10.0. I don't know how many numbers there will be or their format, or even how many spaces there are in between them, but I know that values = { and } will be at the start and end.


Using a regular expression I can find values = { 000.10, 2.00, 5.0, 10.0 } by doing:



import re

string = "Hi, this is a string where only values = { 000.10, 2.00, 5.0, 10.0 } are important"

match = re.search(r'values\s=\s\{.+}\s', string)
if match:
print match.group()
else:
print "Could not find a match..."


Which outputs:


values = { 000.10, 2.00, 5.0, 10.0 }


So how can I get only the text between the patterns r'values\s=\s\{ and \}\s?


I know that I could just replace the starting and end patterns with empty strings like this:


match.group().replace('values = { ', '').replace(' } ', '')


but is there a way to incorporate the fact that I only want the result between two patterns in the regular expression itself?


Hopefully this question makes sense. Any answers would be appreciated.


Aucun commentaire:

Enregistrer un commentaire