给出字符串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)'''
答案 0 :(得分:2)
您可以使用
a[a-zA-Z]+?b
import re
s = input()
matches = re.findall(r'a[a-zA-Z]+?b',s)
print(len(matches))
答案 1 :(得分:1)
使用 <cffunction name="getRegularInvoice" access="remote" output="false" returnformat="json">
<cfargument name="cusno" type="any" required="true">
<cfquery name="getitems" datasource="#datasrc#">
SELECT *
FROM reg_invoice_items
WHERE cusno = '#arguments.cusno#'
ORDER BY code asc
</cfquery>
<cfset returnArray = arrayNew(1)>
<cfloop query="getitems">
<cfset data = structNew() />
<cfset data['refid'] = #getitems.refid# />
<cfset data['code'] = #getitems.code# />
<cfset data['description'] = #getitems.description# />
<cfset data['qty'] = #getitems.qty# />
<cfset data['price'] = #getitems.price# />
<cfset data['total'] = #getitems.total# />
<cfset data['originalprice'] = #getitems.originalprice# />
<cfset data['disc'] = #getitems.disc# />
<cfset data['exvat'] = #getitems.exvat# />
<cfset data['location'] = #getitems.location# />
<cfset data['costnon'] = #getitems.costnon# />
<cfset data['ajustnon'] = #getitems.ajustnon# />
<cfset data['regmonth'] = #getitems.regmonth# />
<cfset data['regday'] = #getitems.regday# />
<cfset data['onceoff'] = "#getitems.once_off#" />
<cfset data['dep'] = #getitems.dep# />
<cfset data['payment_type'] = #getitems.payment_type# />
<cfset data['currency'] = #getitems.currency# />
<cfset arrayAppend(returnArray,data) />
</cfloop>
<cfreturn returnArray />
来匹配所有子字符串:
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))
答案 2 :(得分:1)
您可以在单词中找到a
和b
的位置,找到所有可能的子字符串,然后过滤仅在其中包含一个或多个字符的子字符串
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']
答案 3 :(得分:0)
re.findall(r'a[A-Za-z]+?b',s)
哪里
[A-Za-z]
匹配字母字符,+
是一个或多个字符?
告诉它不贪心答案 4 :(得分:0)
您可以匹配a
,后跟1个字符a-z
,然后使用匹配0+次a
或c-z
的字符类,然后匹配第一个{{1} }
b
如果您要匹配以下所有b以匹配a[a-z][ac-z]*b
而不是aabb
,则可以使用
aab
a[a-z][ac-z]*b+