最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Use a python regex expression to replace spaces between words in string1 with underscore, and add string2 at the end of string1

programmeradmin1浏览0评论

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
  • 2 Why not just string1_revised = f"{string1.replace(' ','_')}_aaa.pdf"? – Wiktor Stribiżew Commented Mar 14 at 0:20
  • do as @WiktorStribiżew suggested or if you must use regex: string1_revised=re.sub('[ ]','_',string1) + '_aaa.pdf' – ticktalk Commented Mar 14 at 0:46
  • Why do you want to use re for this? It's inappropriate and unnecessarily complicated – Adon Bilivit Commented Mar 14 at 8:18
Add a comment  | 

1 Answer 1

Reset to default 3

A 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)

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论