簡體   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