[英]Regex match single characters between strings
I have a string with some markup which I'm trying to parse, generally formatted like this. 我有一个带有一些标记的字符串,我正在尝试解析,通常是这样格式化的。
'[*]\r\n[list][*][*][/list][*]text[list][*][/list]'
I want to match the asterisks within the [list] tags so I can re.sub them as [**] but I'm having trouble forming an expression to grab them. 我想匹配[list]标签中的星号,所以我可以将它们重新设置为[**],但是我很难形成一个表达式来抓住它们。 So far, I have: 到目前为止,我有:
match = re.compile('\[list\].+?\[/list\]', re.DOTALL)
This gets everything within the list, but I can't figure out a way to narrow it down to the asterisks alone. 这可以获得列表中的所有内容,但我无法找到一种方法将其缩小到单独的星号。 Any advice would be massively appreciated. 任何建议都会受到大力赞赏。
You may use a re.sub
and use a lambda in the replacement part. 您可以使用re.sub
并在替换部件中使用lambda。 You pass the match to the lambda and use a mere .replace('*','**')
on the match value. 您将匹配传递给lambda并在匹配值上使用仅仅.replace('*','**')
。
Here is the sample code: 以下是示例代码:
import re
s = '[*]\r\n[list][*][*][/list][*]text[list][*][/list]'
match = re.compile('\[list].+?\[/list]', re.DOTALL)
print(match.sub(lambda m: m.group().replace('*', '**'), s))
# = > [*]
# [list][**][**][/list][*]text[list][**][/list]
See the IDEONE demo 请参阅IDEONE演示
Note that a ]
outside of a character class does not have to be escaped in Python re
regex. 请注意,在Python re
regex中不必转义字符类之外的a ]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.