Python - Locating the position of a regex match in a string?

后端 未结 3 2003
余生分开走
余生分开走 2020-12-02 16:40

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 si

3条回答
  •  -上瘾入骨i
    2020-12-02 17:13

    You could use .find("is"), it would return position of "is" in the string

    or use .start() from re

    >>> re.search("is", String).start()
    2
    

    Actually its match "is" from "This"

    If you need to match per word, you should use \b before and after "is", \b is the word boundary.

    >>> re.search(r"\bis\b", String).start()
    5
    >>>
    

    for more info about python regular expressions, docs here

提交回复
热议问题