"[\w']+|[.,!?;]"
1st Alternative: [\w']+
[\w']+ match a single character present in the list below
Quantifier: + Between one and unlimited times, as many times as possible, giving back as needed [greedy]
\w match any word character [a-zA-Z0-9_]
' the literal character '
2nd Alternative: [.,!?;]
[.,!?;] match a single character present in the list below
.,!?; a single character in the list .,!?; literally
不幸的是,Python不能被空字符串分割。在
要解决这个问题,您需要使用
findall
,而不是split
。在实际上
\b
只是单词边界的意思。在它相当于
(?<=\w)(?=\W)|(?<=\W)(?=\w)
。在这意味着,以下代码将起作用:
输出:
^{pr2}$Ideone Demo
Regex101 Demo
正则表达式说明:
相关问题 更多 >
编程相关推荐