簡體   English   中英

匹配字符串中的唯一模式-Python

[英]Match unique patterns in string - Python

我有一個名為txtFreeForm的字符串列表:

['Add roth Sweep non vested money after 5 years of termination',
 'Add roth in-plan to the 401k plan.]

我需要檢查句子中是否僅存在“添加羅斯”。 為此,我使用了這個

for each_line in txtFreeForm:
    match = re.search('add roth',each_line.lower())
    if match is not None:
        print(each_line)

但這顯然返回了我列表中的兩個字符串,因為它們都包含“ add roth”。 有沒有一種方法可以在句子中專門搜索“添加羅斯”,因為我有很多這樣的模式可以在字符串中搜索。

謝謝你的幫助!

您可以使用字符串的.Length屬性解決此問題嗎? 我不是經驗豐富的Python程序員,但是我認為這應該起作用:

for each_line in txtFreeForm:
    match = re.search('add roth',each_line.lower())
    if (match is not None) and (len(txtFreeForm) == len("Add Roth")):
        print(each_line)

基本上,如果文本在字符串中,並且字符串的長度恰好等於字符串“ Add Roth”的長度,則它必須僅包含“ Add Roth”。

我希望這可以幫到你。

編輯:

我誤會了你的要求。 您要打印包含“添加羅斯”的句子,但不打印包含“在計划中添加羅斯”的句子。 這個對嗎?

這個代碼怎么樣?

for each_line in txtFreeForm:
    match_AR = re.search('add roth',each_line.lower())
    match_ARIP = re.search('add roth in plan',each_line.lower())
    if (match_AR is True) and (match_ARIP is None):
        print(each_line)

這似乎應該解決該問題。 您也可以通過搜索任何字符串並將其添加到比較中來排除任何字符串(例如“計划中”)。

您接近了:)試一試:

for each_line in txtFreeForm:
    match = re.search('add roth (?!in[-]plan)',each_line.lower())
    if match is not None:
        print(each_line[match.end():])

編輯:啊,我讀錯了...你有很多這些。 這需要一些更具侵略性的魔術。

import re
from functools import partial

txtFreeForm = ['Add roth Sweep non vested money after 5 years of termination',
               'Add roth in-plan to the 401k plan.']


def roths(rows):
    for row in rows:
        match = re.search('add roth\s*', row.lower())
        if match:
            yield row, row[match.end():]

def filter_pattern(pattern):
    return partial(lazy_filter_out, pattern)


def lazy_filter(pattern):
    return partial(lazy_filter, pattern)


def lazy_filter_out(pattern, rows):
    for row, rest in rows:
        if not re.match(pattern, rest):
            yield row, rest

def magical_transducer(bad_words, nice_rows):
    magical_sentences = reduce(lambda x, y: y(x), [roths] + map(filter_pattern, bad_words), nice_rows)
    for row, _ in magical_sentences:
        yield row

def main():
    magic = magical_transducer(['in[-]plan'], txtFreeForm)
    print(list(magic))

if __name__ == '__main__':
    main()

為了稍微解釋一下正在發生的事情,您提到您需要處理很多這樣的單詞。 您可以比較兩組項目的傳統方式是使用嵌套的for循環。 所以,

results = []
for word in words:
    for pattern in patterns:
        data = do_something(word_pattern)
        results.append(data)
for item in data:
   for thing in item:
      and so on...
         and so fourth...

我正在使用幾種不同的技術來嘗試實現“扁平化”的實現並避免嵌套循環。 我會盡力描述它們。

**Function compositions**
# You will often see patterns that look like this:
x = foo(a)
y = bar(b)
z = baz(y)

# You may also see patterns that look like this:
z = baz(bar(foo(a)))

# an alternative way to do this is to use a functional composition
# the technique works like this:
z = reduce(lambda x, y: y(x), [foo, bar, baz], a)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM