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

2024-10-01 00:32:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为txtFreeForm的字符串列表:

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

我需要检查句子中是否只有Add roth。为此我用了这个

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

但这显然会返回我列表中的两个字符串,因为它们都包含“add roth”。有没有一种方法可以在一个句子中专门搜索“Add roth”,因为我要在字符串中搜索这些模式。你知道吗

谢谢你的帮助!你知道吗


Tags: 字符串inadd列表matchline句子each
2条回答

你能用字符串的.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”。你知道吗

我希望这是有帮助的。你知道吗

编辑:

我误解了你的要求。你想打印出包含“Add Roth”的句子,而不是包含“Add Roth in plan”的句子。是这样吗?你知道吗

这个代码怎么样?你知道吗

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)

这似乎可以解决问题。通过搜索字符串并将其添加到比较中,可以排除任何字符串(如“in plan”)。你知道吗

你很接近:)试试这个:

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)

相关问题 更多 >