Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python限制重复字母_Python_Regex_String - Fatal编程技术网

python限制重复字母

python限制重复字母,python,regex,string,Python,Regex,String,将重复字母限制为1和2的最佳方法是什么,例如: apple=>aple和苹果 BBBBBBEEEEER=>ber,啤酒,bber,bber 现在,我有这个: a = "hellllllllllooooooooooooo" match = re.search('(.)\\1+', a) if match: print 'found' print re.sub('(.)\\1+', '\\1', a) print re.sub('(.

将重复字母限制为1和2的最佳方法是什么,例如:
apple=>aple和苹果
BBBBBBEEEEER=>ber,啤酒,bber,bber

现在,我有这个:

a = "hellllllllllooooooooooooo"
    match = re.search('(.)\\1+', a)

    if match:
        print 'found'
        print re.sub('(.)\\1+', '\\1', a)
        print re.sub('(.)\\1+', '\\1\\1', a)
    else:
        print 'not found'
但它只会返回:

helo
helloo
如何使其按我想要的方式工作?

导入re
import re

def permute(seq):
    if len(seq) < 2:
        yield seq
    else:
        for tail in permute(seq[2:]):
            yield seq[:2] + tail
            yield seq[:2] + seq[1:2] + tail

text = "hellllllllllooooooooooooo"
seq = re.split('(.)\\1+', text)

for result in permute(seq):
    print ''.join(result)
def排列(序号): 如果len(seq)<2: 收益率序列 其他: 对于排列中的尾部(seq[2:]): 收益率序列[:2]+尾部 收益率序列[:2]+序列[1:2]+尾部 text=“helllllllloooooo” seq=re.split(“()\\1+”,文本) 对于排列结果(序号): 打印“”。加入(结果)
不要为此使用REs。REs用于搜索、匹配和转换,但不用于生成字符串

我们可以考虑一个字符串作为向量;每个字母都是一个维度,重复次数是该维度上组件的长度。给定一个向量V,需要所有可能的向量与V的维数相同,如果V的对应分量为1,则每个分量的值为1,否则为1或2。基于此,这里有一个函数,它可以满足您的需要

def doppelstring(s):
    letter_groups = ((val, list(group)) for val, group in itertools.groupby(s))
    max_vector = ((val, min(len(group), 2)) for val, group in letter_groups)
    vector_components = ([dim * (l + 1) for l in range(maxlen)] for dim, maxlen in max_vector)
    return [''.join(letters) for letters in itertools.product(*vector_components)]
这里有一个更紧凑的版本,使用切片。它的可读性可能稍差,但至少保持在78个字符的限制范围内:

def doppelstring(s):
    max_vs = (''.join(itertools.islice(g, 2)) for k, g in itertools.groupby(s))
    components = ([s[:l + 1] for l in range(len(s))] for s in max_vs)
    return [''.join(letters) for letters in itertools.product(*components)]

这是第一个出现在我脑海中的非正则表达式方式

首先制作一个通用的
挤压
函数:

def squeeze(str, chars='abcdefghijklmnopqrstuvwxyz', min=1): 
    new_str = str
    for c in chars:
        new_str = new_str.replace(c*(1+min),c*min)
    if new_str != str:
        new_str = squeeze(new_str, min=min)
    return new_str

>>> squeeze('aaaabbbbcccc')
'abc'
>>> squeeze('aaaabbbbcccc', min=2)
'aabbcc'
然后,我们可以编写一个小函数,生成每个“压缩排列”,并可用于初始化
集合

def squeezutations(str):
    str = squeeze(str, chars=set(str), min=2)
    for j,k in ((j,k) for j in range(2,0,-1) for k in range(1,3)):
        for c in set(str):
            yield squeeze(squeeze(str, chars=c, min=k), chars=set(str)-set(c), min=j )

>>> set(squeezutations('appppppppple'))
set(['apple', 'aple'])

我不是正则表达式专家,但我突然想到,你需要解决每一个重复的信。现在看来你只是把它当作最近的一封重复信。可能类似于
('('.\\1+)*
?我得到了无效的表达:
(('.\\1+)*
S'为什么我把它作为注释而不是答案:)它主要是为了举例说明理论:你需要捕捉每个重复的字母;不只是最后一个。我不知道它的语法。问题不在于正则表达式,而是必须发出所有可能的单重复和双重复排列。senderle:是的。排列是一种排列;因此,内存密集型(在生成时,计算也很密集)。+1用于阻止在这种情况下使用正则表达式。这种情况是不规则的,因此正则表达式不适用。正则表达式可能不是最好的解决方案,但不是因为问题不是规则的。正则表达式长期以来一直能够匹配非正则语言,如\1这样的反向引用所示。