我有下面的文本字符串,我想写一个正则表达式来匹配字符串模式,如下所示:
[ 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]:]'
输出
关键是正则表达式在字符串中的每个字符之前匹配,因为它是可选的,} quantifier 修改的non-capturing group,使它匹配1到0次。你知道吗
(?:...)?
,用^{而且,
\d
只匹配一个数字,您还需要考虑字母。你知道吗为了快速解决问题,您可以使用
见this regex demo。
\w+
结构匹配1+个单词字符(字母、数字或下划线)。你知道吗如果只匹配
]
之后.
之前的1+个数字或1+个字母,可能会使它更精确:见another regex demo。你知道吗
相关问题 更多 >
编程相关推荐