简体   繁体   English

正则表达式搜索和替换:如何在一个文本块中移动字符

[英]Regex search and replace: How to move characters in a block of text

I'm having a search and replace problem. 我正在搜索和替换问题。 Take this example. 举个例子。

I want to go from: 我想从:

"Word1 word2 =word3 *word4 word5= word6 word7* (*word8)"

To this: 对此:

"Word1 word2 word3= word4* word5= word6 word7* word8*"

ie To replace any word starting a * or = with itself with the * or = moved to the end of the word, and to make it worse sometimes those words are in brackets, and/or could be at the start or end of a line. 即,用*或=替换任何以*或=开头的单词,将*或=移到单词的末尾,有时候这些单词在括号内,和/或可能在一行的开头或结尾。

I've tried to search for the solution but I am relatively new at regex and whilst I can cobble together solutions that find the words I am looking for, eg: 我试图寻找解决方案,但我在正则表达式相对较新,而我可以拼凑出找到我要找的单词的解决方案,例如:

\[\*,\=][a-zA-Z]{1,}[\s,\)]

I can't figure out / understand how to do the replace and maintain end of line / start of line characters, white space and brackets. 我无法弄清楚/理解如何更换和维护行结尾符号,空格和括号的行尾。

I am using Python, but if it makes a material difference I'm happy to try using something else. 我正在使用Python,但如果它有重大差异,我很乐意尝试使用其他东西。

You need 2 capture group and replace them together: 您需要2个捕获组并将它们替换在一起:

>>> import re
>>> 
>>> s = "Word1 word2 =word3 *word4 word5= word6 word7* (*word8)"
>>>
>>> re.sub(r'(\*|=)(\b\w+\b)', r'\2\1', s)
'Word1 word2 word3= word4* word5= word6 word7* (word8*)'

Use a verbose expression like the following: 使用如下所示的详细表达式:

import re
rx = re.compile('''
    \(?    # opening parenthesis or not
    ([*=]) # capture one of * or = to Group 1
    (\w+)  # at least one word chararacter to Group 2
    \)?    # a closing parenthesis
''', re.VERBOSE)

string = "Word1 word2 =word3 *word4 word5= word6 word7* (*word8)"
new_string = rx.sub(r'\2\1', string)


See a demo on ideone.com and add other characters to the class in square brackets ( [...] ) as needed. 查看ideone.com上的演示,并根据需要在方括号( [...] )中将其他字符添加到类中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM