Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个交替正则表达式,我想反转但似乎无法让它工作,它看起来像这样:
( |\w+-\w+| \+\w+|\w)
这将提取除单词中间的 - 或单词前面的 + 之外的所有特殊字符。问题是我想删除此正则表达式未涵盖的所有内容,而是简单的 put 解决方案?!在这前面是行不通的。
样本输入:-xxx xxx- xx-xx +xxx xxx+ xx+xx 期望的输出:xxx xxx xx-xx +xxx xxx xxxx
-xxx xxx- xx-xx +xxx xxx+ xx+xx
xxx xxx xx-xx +xxx xxx xxxx
谢谢您的帮助,
马蒂亚斯
你的问题有点不清楚,你在找这个吗?
a = "abc def,ghi remove - this keep-that foo + bar +keep!" import re print re.sub(r'[^\w\s+-]|(?<!\w)-(?!\w)|\+(?!\w)', '', a) #abc defghi remove this keep-that foo bar +keep
更准确的正则表达式:
[^\w\s+-]|^-|-$|\+$|(?<=\W)-|-(?=\W)|\+(?=\W)|(?<=\w)\+