在 python 字符串中查找具有每个模式组件的灵活长度的模式
Find pattern in python string with flexible length of each pattern component
我有一个字符串:
str_x = "121001221122010120211122211122222222112222"
我想找出给定模式在字符串中出现了多少次,但该模式应该被视为 flexible:
我正在寻找的模式是:
- 至少三个2后跟至少两个1后跟至少三个2
因此,满足此条件的模式将是“22211222”,但也可能是“2222111222”和“222222221111111111222”
我想知道这个"flexible pattern"在str_x中出现了多少次。
这里的正确答案是2次。
知道怎么做吗?非常感谢。
编辑
鉴于我在上面放置的定义,2 次的答案实际上是不正确的,因为有效模式重叠...例如“222111222”、“2221112222”、“22211122222”等都是满足objective的模式。
我要的是求出不重叠的模式数(即还是2次)
您必须使用正则表达式来解决您的问题:
https://docs.python.org/2/library/re.html
正则表达式:
regex = r"2{3,}?1{2,}?2{3,}?"
means = 找到至少三个 2 后跟至少两个 1 后跟至少三个 2
符号 2{3,}
表示找到所有至少三个 2
?
表示——贪心搜索——可能重叠的搜索
如果您想找到不重叠的模式 - 只需删除 ?
import re
regex = r"2{3,}?1{2,}?2{3,}?"
test_str = "121001221122010120211122211122222222112222"
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
print ("total matches: {matches}".format(matches= matchNum))
这是一段有效的代码:
def count_pattern(str):
# one_count keeps count of contiguous 1s
# we check for the pattern at 2 just after a block of 1s
# count keeps track of pattern counts
count=0
one_count=0
for i in range(1,len(str)):
if str[i]=='1':
if str[i-1]=='1':
one_count=one_count+1
else:
one_count=1
elif (str[i]=='2')&(str[i-1]=='1')&(len(str)-i>2)&
(i>one_count+2)&(one_count>1)&(str[(i+1):(i+3)]=='22')&
(str[(i-one_count-3):(i-one_count)]=='222'):
count=count+1
return(count)
print("Number of times the pattern
occurs=",count_pattern('121001221122010120211122211122222222112222'))
我有一个字符串:
str_x = "121001221122010120211122211122222222112222"
我想找出给定模式在字符串中出现了多少次,但该模式应该被视为 flexible:
我正在寻找的模式是:
- 至少三个2后跟至少两个1后跟至少三个2
因此,满足此条件的模式将是“22211222”,但也可能是“2222111222”和“222222221111111111222”
我想知道这个"flexible pattern"在str_x中出现了多少次。
这里的正确答案是2次。
知道怎么做吗?非常感谢。
编辑
鉴于我在上面放置的定义,2 次的答案实际上是不正确的,因为有效模式重叠...例如“222111222”、“2221112222”、“22211122222”等都是满足objective的模式。
我要的是求出不重叠的模式数(即还是2次)
您必须使用正则表达式来解决您的问题: https://docs.python.org/2/library/re.html
正则表达式:
regex = r"2{3,}?1{2,}?2{3,}?"
means = 找到至少三个 2 后跟至少两个 1 后跟至少三个 2
符号 2{3,}
表示找到所有至少三个 2
?
表示——贪心搜索——可能重叠的搜索
如果您想找到不重叠的模式 - 只需删除 ?
import re
regex = r"2{3,}?1{2,}?2{3,}?"
test_str = "121001221122010120211122211122222222112222"
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
print ("total matches: {matches}".format(matches= matchNum))
这是一段有效的代码:
def count_pattern(str):
# one_count keeps count of contiguous 1s
# we check for the pattern at 2 just after a block of 1s
# count keeps track of pattern counts
count=0
one_count=0
for i in range(1,len(str)):
if str[i]=='1':
if str[i-1]=='1':
one_count=one_count+1
else:
one_count=1
elif (str[i]=='2')&(str[i-1]=='1')&(len(str)-i>2)&
(i>one_count+2)&(one_count>1)&(str[(i+1):(i+3)]=='22')&
(str[(i-one_count-3):(i-one_count)]=='222'):
count=count+1
return(count)
print("Number of times the pattern
occurs=",count_pattern('121001221122010120211122211122222222112222'))