簡體   English   中英

使用python re模塊在兩個字符(a * b)之間的字符串中查找子字符串的數量

[英]Find number of sub-string in a string between two character(a*b) using python re module

給定字符串S作為輸入。 程序必須找到匹配a * b的模式數量。 其中*表示1個或多個字母。

import re
s = input()
matches = re.findall(r'MAGIC',s)
print(len(matches))


'''
i/p - aghba34bayyb
o/p - 2
(i.e aghb,ayyb)
It should not take a34b in count.

i/p - aabb
o/p - 3
(i.e aab abb aabb)

i/p : adsbab 
o/p : 2 
(i.e adsb ab)'''

您可以使用

a[a-zA-Z]+?b

在此處輸入圖片說明


import re
s = input()
matches = re.findall(r'a[a-zA-Z]+?b',s)
print(len(matches))

Python Demo

使用re.finditer匹配所有子字符串:

inputs = ['aghba34bayyb',
'aabb',
'adsbab']

import re

def all_substrings(s):
    length, seen = len(s), set()
    for i in range(length):
        for j in range(i + 1, length + 1):
            for g in re.finditer(r'(a[^\d]+b)', s[i:j]):
                if (i+g.start(), i+g.end()) in seen:
                    continue
                seen.add((i+g.start(), i+g.end()))
                yield g.groups()[0]

for i in inputs:
    print('Input="{}" Matches:'.format(i))
    for s in all_substrings(i):
        print(' "{}"'.format(s))

印刷品:

Input="aghba34bayyb" Matches:
 "aghb"
 "ayyb"
Input="aabb" Matches:
 "aab"
 "aabb"
 "abb"
Input="adsbab" Matches:
 "adsb"
 "adsbab"

您可以找到單詞中ab的位置,找到所有可能的子字符串,然后過濾僅在其中包含一個或多個字符的子字符串

from itertools import product

words = ['aghba34bayyb', 'aabb', 'adsbab']

for word in words:
    a_pos = [i for i,c in enumerate(word) if c=='a']
    b_pos = [i for i,c in enumerate(word) if c=='b']
    all_substrings = [word[s:e+1] for s,e in product(a_pos, b_pos) if e>s]
    substrings = [s for s in all_substrings if re.match(r'a[a-zA-Z]+b$', s)]
    print (word, substrings)

輸出量

aghba34bayyb ['aghb', 'ayyb']
aabb ['aab', 'aabb', 'abb']
adsbab ['adsb', 'adsbab']
re.findall(r'a[A-Za-z]+?b',s)

哪里

  • [A-Za-z]匹配字母字符,
  • +是一個或多個字符
  • ? 告訴它不貪心

您可以匹配a后跟1個字符的az ,然后使用匹配0+次乘以acz的字符類,然后匹配第一個b

a[a-z][ac-z]*b

正則表達式演示

如果要匹配所有以下b以匹配aabb而不是aab ,則可以使用

a[a-z][ac-z]*b+

正則表達式演示 | Python演示

import re
s = input()
matches = re.findall(r'a[a-z][ac-z]*b+',s)
print(len(matches))

暫無
暫無

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

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