繁体   English   中英

搜索严格格式的字符串

[英]Searching for a string within a strict format

我想使用 python re 库搜索具有以下格式的子字符串:

(some word)(\)term1(\)(some word) (some word)(\)term2(\)(some word)

括号中的组是可选的,term1 和 term2 必须在该格式的字符串中。

它应该检测的一些示例:

  • random sentence word\term1 term2 end of random sentence
  • random sentence term1 term2 end of random sentence
  • random sentence word\term1\word word\term2\word end of random sentence

到目前为止,我已经尝试过:

r'((\W+|^)term1((\W))*)(\w+|) (\w+|)(\W|)term2(\W|)'

但它不起作用

我的猜测是,也许

^(\([^)]*\))?(\(\\\))?term 1(\(\\\))?(\([^)]*\))?\s(\([^)]*\))?(\(\\\))?term 2(\(\\\))?(\([^)]*\))?$

可能会奏效。

演示

这种模式应该有效:

^[\w ]*\\?term1\\?[\w ]*\\?term2\\?[\w ]*$

Python 演示:

import re

pattern = re.compile(r"^[\w ]*\\?term1\\?[\w ]*\\?term2\\?[\w ]*$")

string1 = r"random sentence word\term1 term2"
string2 = r"random sentence term1 term2 end of random sentence"
string3 = r"random sentence word\term1\word word\term2\word end of random sentence"

print(bool(re.search(pattern, string1)))
print(bool(re.search(pattern, string2)))
print(bool(re.search(pattern, string3)))

Output:

 True True True

使用以下内容:

^.*\s(?:\w+\\)?term1(?:\\\w+)?\s(?:\w+\\)?term2(?:\\\w+)?\s.*$

演示和解释

import re

lines = [
    r'random sentence word\term1 term2 end of random sentence',
    r'random sentence term1 term2 end of random sentence',
    r'random sentence word\term1\word word\term2\word end of random sentence'
]

regex = re.compile(r'(\b\w+\b)?\\?term1\\?(\b\w+\b)? (\b\w+\b)?\\?term2\\?(\b\w+\b)?')
for line in lines:
    m = regex.search(line)
    if m:
        print('Match:', m.group(0))
    else:
        print("No match")

印刷:

Match: word\term1 term2
Match: term1 term2
Match: word\term1\word word\term2\word

在此处输入图像描述

暂无
暂无

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

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