仅考虑第一个匹配项(Python重新)

时间:2018-08-04 07:49:42

标签: python regex

字符串中以给定字符串开头和结尾的不同子字符串

给出一个字符串s和另外两个字符串的开始和结束,找到该字符串中以给定的begin和end字符串开始和结束的不同子字符串的数量。

示例:

Input : s = "geeksforgeeks"
        begin = "geeks"
        end = "for"
Output : 1

Input : s = "vishakha"
        begin = "h"
        end = "a"
Output : 2
Two different sub-strings are "ha" and "hakha".

到目前为止我的方法

import re

def find_from_right(string,reg):
    s = ""
    match = []
    for c in string:
        s = s+c
        for i in re.findall(reg, s):
            match.append(i)
    return match

print(find_from_right(str(raw_input()),r".*c.*d$"))

输出: ['cod', 'codecppforfood']

问题在于它与以下字符串不匹配:

cppforfood

它仅考虑第一个比赛。

有人可以指出错误吗?

2 个答案:

答案 0 :(得分:1)

您可以将re.compilere.findallregex模块结合使用,以找到重叠的匹配项。

import regex 

s = "geeksforgeeks"
begin = "geeks"
end = "for"

first_pattern = regex.compile(''.join(begin+'[a-zA-Z]*'+end))
print(regex.findall(first_pattern,s,overlapped=True));

s = "vishakha"
begin = "h"
end = "a"

second_pattern = regex.compile(''.join(begin+'[a-zA-Z]*'+end))
print(regex.findall(second_pattern,s,overlapped=True));

上面打印的代码:

['geeksfor']
['hakha', 'ha']

答案 1 :(得分:1)

您可以使用正则表达式查找字符串中所有出现的开始和结束符号,并使用列表推导构造答案:

s = input("Enter String: ")
head = input("Enter start: ")
tail = input("Enter end: ")

result = list(set([s[i.start():j.start() + 1] 
                   for i in re.finditer(head, s) for j in re.finditer(tail, s)
                   if i.start() < j.start()]))

print(result)

输出

['cod', 'codecppforfood', 'cppforfood']