admin管理员组文章数量:1391955
I want to use a Python regex replacement operation to change prospective file names such as
Using 3 Buttons in PyQt5 DialogButtonBox
into
Using_3_Buttons_in_PyQt5_DialogButtonBox_aaa.pdf
If I only wanted to replace the spaces with "_", the replacement operation would be trivial and could be solved with the following lines:
string1 = "Using 3 Buttons in PyQt5 DialogButtonBox"
string1_revised = re.sub('[ ]', '_', string1)
If I wanted to only add '_aaa.pdf' to the end of string1, the operation again would trivial:
result2 = re.sub('(?P<wholeThing>.+)', '\g<wholeThing>_aaa.pdf', string1)
But I want the replacement operation to replace the spaces with underscores, and add "_aaa.pdf"
to the end of string1_revised
.
I think the following expression matches string1
, and assigns the group name "wholeThing"
to string1
: (?P<wholeThing>(([A-Za-z0-9]+)([ ]))(x)?)
But I can't figure out how to code the replacement. Any insights would be much appreciated. Thank you. Marc
I want to use a Python regex replacement operation to change prospective file names such as
Using 3 Buttons in PyQt5 DialogButtonBox
into
Using_3_Buttons_in_PyQt5_DialogButtonBox_aaa.pdf
If I only wanted to replace the spaces with "_", the replacement operation would be trivial and could be solved with the following lines:
string1 = "Using 3 Buttons in PyQt5 DialogButtonBox"
string1_revised = re.sub('[ ]', '_', string1)
If I wanted to only add '_aaa.pdf' to the end of string1, the operation again would trivial:
result2 = re.sub('(?P<wholeThing>.+)', '\g<wholeThing>_aaa.pdf', string1)
But I want the replacement operation to replace the spaces with underscores, and add "_aaa.pdf"
to the end of string1_revised
.
I think the following expression matches string1
, and assigns the group name "wholeThing"
to string1
: (?P<wholeThing>(([A-Za-z0-9]+)([ ]))(x)?)
But I can't figure out how to code the replacement. Any insights would be much appreciated. Thank you. Marc
Share Improve this question edited Mar 14 at 0:21 Wiktor Stribiżew 628k41 gold badges498 silver badges615 bronze badges asked Mar 14 at 0:00 Marc B. HankinMarc B. Hankin 7693 gold badges19 silver badges37 bronze badges 3 |1 Answer
Reset to default 3A regex approach for this problem is a bit an overkill but it come out smt interesting, at least for me, that
\Z
is a regex symbol (toward the end of the section) to denote the end of the string''
(the empty character) is its corresponding match group
Then you can do a look-up dictionary between the matching objects and the substitution texts that can be passed to the repl
callable argument of re.sub
.
text = #
mapping = {
' ': '_',
'': '_aaa.pdf' # key: matching group of \Z
}
re.sub(r'( |\Z)', lambda m: mapping[m.group()], text)
本文标签:
版权声明:本文标题:Use a python regex expression to replace spaces between words in string1 with underscore, and add string2 at the end of string1 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744679393a2619306.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
string1_revised = f"{string1.replace(' ','_')}_aaa.pdf"
? – Wiktor Stribiżew Commented Mar 14 at 0:20