Python - Find where in string regex match was found?

Posted by nb on Stack Overflow See other posts from Stack Overflow or by nb
Published on 2010-04-20T10:49:00Z Indexed on 2010/04/20 10:53 UTC
Read the original article Hit count: 156

Filed under:
|

I'm currently using regular expressions to search through RSS feeds to find if certain words and phrases are mentioned, and would then like to extract the text on either side of the match as well. For example:

String = "This is an example sentence, it is for demonstration only"
re.search("is", String)

I'd like to know where the is was found so that I can extract and output something like this:

1 match found: "This is an example sentence"

I know that it would be easy to do with splits, but I'd need to know what the index of first character of the match was in the string, which I don't know how to find

© Stack Overflow or respective owner

Related posts about python

Related posts about regex