如何使用python编写正则表达式来匹配下面的字符串?

2024-06-16 21:02:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有下面的文本字符串,我想写一个正则表达式来匹配字符串模式,如下所示:

[ 1.1 ] 1. A method of providing a master
[ 12.1 ] 12. An apparatus for providing
[ 39.3 ] b. one or more control point applications
[ 39.8 ] iv. a server application programming interface
[ 30.2 ] a. a client application programming

我想用]来代替] 1.,同样地用] 12.] b.] iv.] a.

请包括一个案件时,下面的事情发生在正常的表达,即如果没有上述模式发生

[ 1.2 ] an RFID device provided

我试过正则表达式,但没用。你知道吗

>>> st = "[ 12.1 ] 12. An apparatus for providing a master content directory within a network of devices comprising:"
>>> import re
>>> st = re.sub(r"(?:\]\s*\d+\.\s*)?","]",st)
>>> st
'][] ]1]2].]1] ]]A]n] ]a]p]p]a]r]a]t]u]s] ]f]o]r] ]p]r]o]v]i]d]i]n]g] ]a] ]m]a]s]t]e]r] ]c]o]n]t]e]n]t] ]d]i]r]e]c]t]o]r]y] ]w]i]t]h]i]n] ]a] ]n]e]t]w]o]r]k] ]o]f] ]d]e]v]i]c]e]s] ]c]o]m]p]r]i]s]i]n]g]:]'

Tags: of字符串文本remasteranforapplication
2条回答
s = """
[ 1.1 ] 1. A method of providing a master
[ 12.1 ] 12. An apparatus for providing
[ 39.3 ] b. one or more control point applications
[ 39.8 ] iv. a server application programming interface
[ 30.2 ] a. a client application programming
"""
print(re.sub(r'\]\s\w{1,2}\.', '] ', s))

输出

[ 1.1 ]  A method of providing a master
[ 12.1 ]  An apparatus for providing
[ 39.3 ]  one or more control point applications
[ 39.8 ]  a server application programming interface
[ 30.2 ]  a client application programming

关键是正则表达式在字符串中的每个字符之前匹配,因为它是可选的,(?:...)?,用^{} quantifier修改的non-capturing group,使它匹配1到0次。你知道吗

而且,\d只匹配一个数字,您还需要考虑字母。你知道吗

为了快速解决问题,您可以使用

st = re.sub(r"\]\s*\w+\.\s*", "] ", st)

this regex demo\w+结构匹配1+个单词字符(字母、数字或下划线)。你知道吗

如果只匹配]之后.之前的1+个数字或1+个字母,可能会使它更精确:

st = re.sub(r"\]\s*(?:\d+|[a-zA-Z]+)\.\s*", "] ", st)
                   ^^^^^^^^^^^^^^^^^

another regex demo。你知道吗

相关问题 更多 >