简体   繁体   English

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

[英]How to write a regular expression to match some below string using python?

I have below text string and I want to write a regular expression to match a string pattern as below: 我下面有文本字符串,我想编写一个正则表达式以匹配如下所示的字符串模式:

[ 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

I want to substitute the ] 1. by ] and similarly for ] 12. , ] b. 我想用[ ] 1.]代替] 1.用类似的方式代替] 12.] b. , ] iv. ] iv. , ] a. ] a.

Please include a case when below thing occur in regular expresison ie if no above pattern occurs 请包括以下情况:正常情况下发生以下情况,即没有发生以上情况

[ 1.2 ] an RFID device provided

I tried below regular expression but it not worked. 我尝试使用正则表达式以下的方法,但是没有用。

>>> 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]:]'
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))

Output 产量

[ 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

The point is that your regex matches before each char in a string because it is optional, (?:...)? 关键是您的正则表达式在字符串中的每个字符之前都匹配,因为它是可选的(?:...)? , a non-capturing group modified with a ? ,用?修改的非捕获组 quantifier , makes it match 1 or 0 times. 量词 ,使其匹配1或0次。

Also, \\d only matches a digit, and you need to consider letters, too. 此外, \\d仅与数字匹配,并且您也需要考虑字母。

To quickly fix the issue you may use 为了快速解决您可能使用的问题

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

See this regex demo . 请参阅此正则表达式演示 The \\w+ construct matches 1+ word chars (letters, digits or underscores). \\w+构造匹配1个以上的字符字符(字母,数字或下划线)。

You may make it a bit more precise if you match just 1+ digits or 1+ letters after ] before . 如果仅在]之前匹配1+个数字或1+个字母,则可以使它更精确一些. :

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

See another regex demo . 参见另一个正则表达式演示

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

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