[英]Python: re.sub pattern and replacement wildcard
I have been going this bit of code for what seems to be a lifetime and cannot seem to get it to work. 我一直在走这段代码,看起来似乎是一辈子的,似乎无法使其正常工作。
pattern = "\[([a-zA-Z0-9].*?)#([a-zA-Z0-9].*?)\]"
pattern_obj = re.compile(pattern, re.MULTILINE)
translation = pattern_obj.sub("<ol>\\1</ol>", translation)
What I am trying to do here is change some text, ie: 我在这里尝试做的是更改一些文本,即:
[
# This is item number one in an ordered list. #
# And this is item number two in the same list. #
]
Into: 进入:
<ol>
#This is item number one in an ordered list. #
#And this is item number two in the same list. #
</ol>
Essentially, it is supposed to identify any text between [ and ] with a # somewhere in the text, and change the [ into <ol>
and ] into </ol>
whilst keeping all internal text the same. 本质上,应该使用文本中的#标识[和]之间的任何文本,并在将所有内部文本保持不变的同时将[更改为
<ol>
和]更改为</ol>
。 Can anyone please advise? 任何人都可以请教吗?
Thank you in advance! 先感谢您!
This does almost what you want: 这几乎可以满足您的要求:
>>> re.compile(r"\[([^\]]*)\]").sub("<ol>\\1</ol>", "b[#a]c")
'b<ol>#a</ol>c'
The [^\\]]
take every character except ] after the \\[
clsing bracket. [^\\]]
会在\\[
括号后加上除\\[
]以外的所有字符。
With an the #
it would look like this: 使用
#
,它将看起来像这样:
>>> re.compile(r"\[([^\]]*#[^\]]*)\]").sub("<ol>\\1</ol>", "b[#a]c")
'b<ol>#a</ol>c'
>>> re.compile(r"\[([^\]]*#[^\]]*)\]").sub("<ol>\\1</ol>", "b[gggg]c")
'b[gggg]c'
The .
的
.
is always a bit of a problem if you want to find something between something. 如果您想在某物之间找到某物,总是有点问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.