Import re
x = 'my website name is www.algoexpert.com and i have other website too'
For line in x:
      y = line.rstrip()
z = re.findall('.*\S+/.[a-z]{0-9}/.\S+',y)
print(z)



  我只想打印网站名称(www.algoexpert.com)

最佳答案

要解决的问题:


x本身就是字符串,为什么要用for line in x对其进行循环?
[a-z]{0-9}-尝试仅覆盖字母字符,尽管使用错误的方式(可能是{0,9})。字符范围应为[a-z0-9]+或至少-[a-z]+(取决于初始意图)
点/句号.应使用反斜杠\.进行转义


固定版本(简体):

import re

x = 'my website name is www.algoexpert.com and i have other website too'
z = re.findall('\S+\.[a-z0-9]+\.\S+', x.strip())
print(z)   # ['www.algoexpert.com']

10-05 19:51