【问题标题】:Using OR operator in re.search for multiple strings in python 3在 re.search 中使用 OR 运算符在 python 3 中搜索多个字符串
【发布时间】:2015-02-11 03:27:38
【问题描述】:

对不起,如果这已经被问到了,虽然我找不到它。我试图检测网站是否使用 wordpress。这是示例代码。

url = input("Please enter the URL")
if 'http://' in url:
    append = url
else:
    append = 'http://' + url
r = requests.get(append + "/robots.txt")
    response = r.text
if re.search(('/wp-includes/') | ('/wp-admin/'), response):
    print("{0} --> Its Wordpress ".format(append))
    sys.exit(0)

上面写着 |运算符不能使用,在

if re.search(('/wp-includes/') | ('/wp-admin/'), response): 

如何搜索多个字符串,它们之间有 OR?我也尝试过使用“或”并尝试过这个

if re.search('/wp-includes/' , '/wp-admin/'), response):

谢谢

【问题讨论】:

    标签: regex search python-3.x


    【解决方案1】:

    正则表达式模式是单个字符串。

    '(foo|bar)'
    

    【讨论】:

      【解决方案2】:

      如何搜索多个字符串,它们之间有 OR?

      使用交替运算符|

      if re.search(r'/wp-(?:admin|includes)/', response):
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-25
        • 1970-01-01
        • 1970-01-01
        • 2023-01-19
        • 1970-01-01
        • 1970-01-01
        • 2013-10-13
        • 1970-01-01
        相关资源
        最近更新 更多