import re
# 1st pattern: vHELLOv
re.findall(r"v(.*?)v", "vHELLOv")
# 2nd pattern: /HELLO/
re.findall(r"/(.*?)/", "/HELLO/")
## NOTE: For regex in python you do not have to escape / with a \.
# But, if you want to use a similar regex universally elsewhere,
# consider escaping each / with a \.
#
# Example: r"/(.*?)/" ==> r"\/(.*?)\/"
试试这个。使用
regex
(正则表达式)库进行模式匹配编辑:按照Jan的建议,在
()
中添加了惰性量词.*?
,而不是.*
模式:
r"v(.*)v"
贪婪-->Example on regex101.com模式:
r"v(.*?)v"
懒惰-->Example on regex101.com更好要删除标点符号,可以在Python中使用以下正则表达式:
如果您谈论的是围绕另一个字符串的两个相同字符串,则可以使用split方法:
相关问题 更多 >
编程相关推荐