有没有一种方法可以检测python字符串中是否有2个或更多的符号相邻,因为我是从tkinker条目中获取数据的,它可以用一个符号替换多个符号?例如,如果用户输入3 +++ 3,它将只用1+处理它,就像3+3一样?我尝试过(.)\1,但它只适用于配对。您可以尝试here帮助将不胜感激。感谢您的支持。
发布于 2020-04-14 16:27:46
你离解决方案真的很近了!您可以使用this answer支持多个对:
# \1+ means: "one or more repetition of the text matched by the first group"
# so this results in 2+ repetition of the same letter here
(.)\1+当然,您可以通过将.更改为[=+-]来缩小要检测的重复字符的范围。
下面是带有一些单元测试的正则表达式:https://regex101.com/r/KhUoyy/1
https://stackoverflow.com/questions/61203431
复制相似问题