简体   繁体   English

搜索严格格式的字符串

[英]Searching for a string within a strict format

I want to search for a sub-string using the python re library with the following format:我想使用 python re 库搜索具有以下格式的子字符串:

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

The groups in brackets are optional, term1 and term2 must be in the string within that format.括号中的组是可选的,term1 和 term2 必须在该格式的字符串中。

A few examples of what it should detect:它应该检测的一些示例:

  • 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

so far i have tried this:到目前为止,我已经尝试过:

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

but it does not work但它不起作用

My guess is that, maybe我的猜测是,也许

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

might work.可能会奏效。

Demo演示

This pattern should work:这种模式应该有效:

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

Python demo: 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: Output:

 True True True

Use the following:使用以下内容:

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

Demo & explanation演示和解释

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")

Prints:印刷:

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