Мы могли бы решить эту проблему, используя список символов с границами слов, возможно, с выражением, похожим на:
(?:.+?)(\b[A-Za-z=;\s]+\b)
Если мы хотим иметь больше символов, мы добавим их к:
[A-Za-z=;\s]
Здесь мы не собираем нежелательные символы с помощью группы без захвата:
(?:.+?)
затем мы собираем желаемые символы, заключенные в группу захвата, которую мы можем просто назвать, используя $1
:
(\b[A-Za-z=;\s]+\b)
Test
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?:.+?)(\b[A-Za-z=;\s]+\b)"
test_str = "command --m=psrmcc;ld - --kkk gtodf --klfj"
subst = "\\1\\n"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 0, re.MULTILINE)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
RegEx Circuit
jex.im визуализирует регулярные выражения:
![enter image description here](https://i.stack.imgur.com/U1jIS.png)