我正在寻找适合ready-to-work
,ready-to-work-today
,ready-to
的正则表达式。我一直以这种方式放置多个选项:
text=''' ready-to-work ready-to ready-to-work-today'''
re.findall(r'\b[a-zA-Z0-9]+?-[a-zA-Z0-9]+?-[a-zA-Z0-9]+?\b', text)
我设法找到了最多三个术语的表达式,例如['ready-to-work']
,但是我无法捕获ready-to
和ready-to-work-today
。我希望['ready-to-work','ready-to','ready-to-work-today']
。
感谢您的帮助。
答案 0 :(得分:1)
尝试使用此正则表达式\b\w+(-\w+)+\b
。
答案 1 :(得分:0)
如果要将连字符和字符类的部分匹配1-3次,则可以将非捕获组与量词{1,3}
配合使用。要匹配1次以上,请使用+
。
\b[a-zA-Z0-9]+(?:-[a-zA-Z0-9]+){1,3}\b
\b
字边界[a-zA-Z0-9]+
匹配列出的1次以上任何时间(?:
非捕获组
-[a-zA-Z0-9]+
匹配-
,并且是列出的任意项的1倍以上){1,3}
关闭非捕获组并重复1-3次\b
字边界答案 2 :(得分:0)
我猜测此表达式可能返回所需的输出:
\b(?:\w+-\w+(?:[\w-])*)\b
re.findall
import re
regex = r"\b(?:\w+-\w+(?:[\w-])*)\b"
test_str = "ready-to-work ready-to-work-today ready-to ready-to-work ready-to ready-to-work-today ready-to ready-to-work-todayready-to ready-to-work-today"
print(re.findall(regex, test_str))
['ready-to-work', 'ready-to-work-today', 'ready-to', 'ready-to-work', 'ready-to', 'ready-to-work-today', 'ready-to', 'ready-to-work-todayready-to', 'ready-to-work-today']
如果要浏览/简化/修改该表达式,请在this demo的右上角进行解释。