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)
string1_revised = f"{string1.replace(' ','_')}_aaa.pdf"
? – Wiktor Stribiżew Commented Mar 14 at 0:20