2014-11-21 66 views
-1

我试图找到字符串中的一组字符的所有组合,但发现的字符集必须具有特定的长度。我曾想过使用set函数[]和函数{:}的出现次数,我似乎无法让它们一起工作。Python正则表达式:设置前缀长度的组合

示例应该返回'this'和'hit'

谢谢。

def test_patterns(text, patterns=[]): 
    """Given source text and a list of patterns, look for 
    matches for each pattern within the text and print 
    them to stdout. 
    """ 
    # Look for each pattern in the text and print the results 
    for pattern, desc in patterns: 
     print "Pattern %r (%s)\n" % (pattern, desc) 
     print ' %r' % text 
     for match in re.finditer(pattern, text): 
      s = match.start() 
      e = match.end() 
      #substr = match.group() 
      substr = text[s:e] 
      n_backslashes = text[:s].count('\\') 
      prefix = '.' * (s + n_backslashes) 
      print ' %s%r' % (prefix, substr) 
     print 
    return 
if __name__ == '__main__': 
    test_patterns('Does this text contain hits or matches?', [('[this]{4:4}+', "Description"),]) 
raw_input() 
+0

好吧,我不知道。 – grasshopper 2014-11-21 11:13:35

+0

例子?你是指这个:“这个文本是否包含命中或匹配?” – grasshopper 2014-11-21 11:14:53

+0

@Kasra从下面的答案,似乎他们一起工作。 – grasshopper 2014-11-21 11:19:09

回答

1

的quatifier {m} quatifies以下正则表达式m

例如

>>> st="Does this text contain hits or matches" 
>>> re.findall(r'[this]{4}', st) 
['this', 'hits'] 

或者更具体

>>> re.findall(r'\b[this]{4}\b', st) 
['this', 'hits'] 

其是相同书写

>>> re.findall(r'\b[this]{4,4}\b', st) 
['this', 'hits'] 
+0

好吧,{4}({m})有效,但{4:4}({m:n})不会。我认为m是重复的最小次数,n是最大值。 – grasshopper 2014-11-21 11:17:34

+0

@grasshopper其{m,n}对于最小m和最大n – nu11p01n73R 2014-11-21 11:18:29

+0

是的,那么为什么{m}找到两个匹配,但{m:m}不是。不应该{m:m}与{m}相同吗? – grasshopper 2014-11-21 11:20:51