简体   繁体   English

根据它们所在的位置用两个不同的字符串替换一个子字符串

[英]Replace a substring with two different strings depending on where they are

Let's say I have a string with some asterisks:假设我有一个带星号的字符串:

myvar = "this is an *italicized* substring"

I want to replace *italicized* with {i}italicized{/i} for the project I'm working on, txt2rpy, but I'm not sure how to have two different substrings being replaced depending on what order they come in.我想更换*italicized*{i}italicized{/i}的项目我的工作,txt2rpy,但我不知道怎么有两个不同的子取决于什么命令他们在被替换。

You can use a regular expression to substitute the pattern as a whole:您可以使用正则表达式来替换整个模式:

re.sub(r'\*(.*?)\*', r'{i}\1{/i}', myvar)

In the regexp:在正则表达式中:

  • \\* matches a literal * (used twice) \\*匹配文字* (使用两次)
  • (.*?) matches any number of any (non-newline) characters, as few as possible - it is also in a capture group (.*?)匹配任意数量的任何(非换行符)字符,尽可能少 - 它也在捕获组中

In the replacement:在替换中:

  • {i} and {/i} are literals {i}{/i}是文字
  • \\1 means to put what was in the first (and in this case, only) capture group \\1意味着把什么放在第一个(在这种情况下,只有)捕获组

This gives:这给出:

>>> import re
>>> myvar = "this is an *italicized* substring"
>>> print(re.sub(r'\*(.*?)\*', r'{i}\1{/i}', myvar))
this is an {i}italicized{/i} substring

If you have more than one occurrence of the pattern, that will work also:如果模式出现不止一次,这也将起作用:

myvar = "this is an *italicized* substring, and here is *another* one"

will give会给

this is an {i}italicized{/i} substring, and here is {i}another{/i} one

You can use re.sub with capture groups for that:您可以将re.sub与捕获组一起使用:

import re

txt = "this is an *italicized* substring"

res = re.sub(r"\*([^*]+)\*", "{i}\g<1>{/i}", txt)

will have res as:将有res为:

this is an {i}italicized{/i} substring

This pattern is pretty basic: It matches a literal * , then character not an asterisk, then another literal * .这个模式非常基本:它匹配一个文字* ,然后是字符而不是星号,然后是另一个文字* The main point here is that we use a capture group to catch the word part.这里的要点是我们使用捕获组来捕获单词部分。

Then we simply substitute the full match with the word we saved (accessed by \\g<1> ) surrounded with your wanted characters.然后我们简单地用我们保存的单词(通过\\g<1>访问)替换完整匹配,并用您想要的字符包围。


Demo here演示在这里

创建一个用于解析的循环并保持一个计数器进行跟踪,每当计数器保持第二个标签并用第一个标签替换奇数时。

you could you use a for loop and say你能不能用一个 for 循环然后说

myvar = "this is an *italicized* substring"
positions = []
for x in range(len(myvar)):
    if myvar[x] == "*":
       positions.append(x)
inAsteriks = myvar[0:positions[0]] + "{i}" + myvar[positions[0]+1:positions[1]] + "{/i}" + myvar[positions[0]+1:]

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

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