I'm trying to extract the position (index) of a substring using regex. I need to use regex because the string won't be exactly the same. I want to get the position of the substring (either starting or ending position), so I can take the 1,000 characters following that substring.
For example, if I had "while foreign currencies are traded frequently, very little money is made by most." I want to find the position of "foreign currencies" so I can get all the words after.
f5 is the text.
I've tried:
p = re.compile("((^\s*|\.\s*)foreign\s*(currency|currencies))?")
for m in p.finditer(f5):
print m.start(), m.group()
to get the location. This gives me (0,0) even though I've checked to make sure the regex picks up what I'm looking for in the text.
I've also tried:
location = re.search(r"((^\s*|\.\s*)foreign\s*(currency|currencies))?", f5)
print location
Output is <_sre.SRE_Match at 0x297d3328>
If I try
location.span()
I get (0,0) again.
Basically, I want to convert <_sre.SRE_Match at 0x297d3328> into an integer that gives the location of the search term.
I've spent half a day searching for a solution. Thanks for any help.
f5
which doesn't work which should? – DSM May 13 '14 at 15:27group()
, for one instance. – Signus May 13 '14 at 15:38