Python正则表达式:查找“com”或“org”

2024-09-26 22:49:54 发布

您现在位置:Python中文网/ 问答频道 /正文

只想找到第一次出现的“com”或“org”。 我试过:

comIndex = domain.index(r '(?: com|org)')

但它不起作用。有人能纠正我吗?你知道吗


Tags: orgcomindexdomaincomindex
3条回答
import re

comIndex = -1
m = re.search(r'(?:com|org)', domain)
if m:
    comIndex = m.start()
print comIndex

我认为你不能像这样使用正则表达式。Python中的Regex不是内置特性,您需要import the ^{} module才能使用其中的方法。你知道吗

import re
...
comMatch = re.search('com|org', domain)
if comMatch:
   comIndex = comMatch.start()

怎么样

re.search('(com)|(org)',domain).span()[0]

相关问题 更多 >

    热门问题