正则表达式,用于匹配以[开头和以]结尾的行

时间:2019-05-30 22:12:43

标签: python regex regex-lookarounds regex-group regex-greedy

我试图在文件中找到以[开头和]结束的行。我正在使用正则表达式,但无法获得结果。

我尝试了带有各种选项的正则表达式,例如\ s,\ S,\ w和\ W。

import re
infile=open("C:\\Users\\Downloads\\Files\\processed.csv","r") 
myregex = re.compile(r'(^\[)(\]$)') 
list=[] 
for groups in myregex.findall(infile.read()):
    item=''.join(groups)
    cleanitem=item.replace('\n','')
    list.append(cleanitem)
print (list)
infile.close()

它应该打印以[开头和以]结尾的所有行。

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

在这里,我们可以找到一个带有捕获组的简单表达式,如果需要的话,它类似于:

^(\[.+\])$

测试

# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility

import re

regex = r"^(\[.+\])$"

test_str = ("[ and ends with ]\n"
    " [ and ends with ]")

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))

# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.

DEMO

RegEx

如果不需要此表达式,可以在regex101.com中对其进行修改/更改。

RegEx电路

jex.im可视化正则表达式:

enter image description here