小编nb.*_*nb.的帖子

Python - 在字符串中定位正则表达式匹配的位置?

我目前正在使用正则表达式来搜索RSS提要,以查找是否提到了某些单词和短语,然后还要在匹配的任一侧提取文本.例如:

String = "This is an example sentence, it is for demonstration only"
re.search("is", String)
Run Code Online (Sandbox Code Playgroud)

我想知道找到'是'匹配的位置,以便我可以提取和输出这样的东西:

1 match found: "This is an example sentence"
Run Code Online (Sandbox Code Playgroud)

我知道分割会很容易,但是我需要知道匹配的第一个字符的索引在字符串中是什么,我不知道如何查找

python regex

43
推荐指数
3
解决办法
7万
查看次数

标签 统计

python ×1

regex ×1