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

后端 未结 3 1930
余生分开走
余生分开走 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条回答
  • 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

    0 讨论(0)
  • 2020-12-02 17:14

    re.Match objects have a number of methods to help you with this:

    >>> m = re.search("is", String)
    >>> m.span()
    (2, 4)
    >>> m.start()
    2
    >>> m.end()
    4
    
    0 讨论(0)
  • 2020-12-02 17:20

    I don't think this question has been completely answered yet because all of the answers only give single match examples. The OP's question demonstrates the nuances of having 2 matches as well as a substring match which should not be reported because it is not a word/token.

    To match multiple occurrences, one might do something like this:

    iter = re.finditer(r"\bis\b", String)
    indices = [m.start(0) for m in iter]
    

    This would return a list of the two indices for the original string.

    0 讨论(0)
提交回复
热议问题