繁体   English   中英

正则表达式查找以[]开头的子字符串

[英]Regex to find substring starting with [ ]

下面是我获得的更大字符串(detaildesc_final)中存在的示例子字符串。 我需要对字符串使用正则表达式搜索,以便可以从[Data]部分检索所有以“ []”(我的意思是两个方括号)开头的行。 在[Data]部分中应检索所有行,直到遇到[Logs]行。

[Data]

[] some text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[Logs]

我正在使用Python处理代码,并且使用了以下命令(这显然是不正确的)。

re.findall(r'\b\\[\\]\w*', detaildesc_final)

我需要结果采用以下格式:

some text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

我已经在网上看到了很多东西,因此可以找出以单个双字符而不是两个(在这种情况下为[])开头的任何行。 任何帮助将不胜感激。 谢谢。

不要使事情过于复杂。

for line in detaildesc_final.split('\n'):
    if line.startswith('[]'):
        do_something()
import re

str = """
[Data]

[] some text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[Logs]
"""


print re.sub("([[a-zA-Z ]{0,}][ ]?)", '',str)

输出:

some text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

some_other_text

您需要正面评价:

import re

pattern=r'(?<=\[\])(.\w.+)'

string_1="""[Data]

[] some text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[] some_other_text

[Logs]"""


match=re.finditer(pattern,string_1,re.M)
for item in match:
    print(item.group(1))

输出:

 some text
 some_other_text
 some_other_text
 some_other_text
 some_other_text
 some_other_text
 some_other_text
 some_other_text
 some_other_text
 some_other_text
 some_other_text
 some_other_text

正则表达式说明:

Positive Lookbehind (?<=\[\])

它告诉正则表达式引擎暂时向后退字符串,以检查后面的内部文本是否可以匹配。

  • \\[匹配字符[从字面上(区分大小写)
  • \\]从字面上匹配字符] (区分大小写)
  • . 匹配任何字符(行终止符除外)
  • \\w匹配任何单词字符(等于[a-zA-Z0-9_]
  • +量词匹配一次和无限次,尽可能多地匹配,并根据需要返回(贪婪)
import re
re.findall(r'\[\] (.*)\n\n', detaildesc_final)

输出:

['some text',
 'some_other_text',
 'some_other_text',
 'some_other_text',
 'some_other_text',
 'some_other_text',
 'some_other_text',
 'some_other_text',
 'some_other_text',
 'some_other_text',
 'some_other_text',
 'some_other_text']

暂无
暂无

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

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