2015-01-05 43 views
0

這裏是我的代碼:re.compile不匹配我的字符串

def split(content): 
    pattern = re.compile(r"""(\\\[-16pt]\n)(.*?)(\n\\\nthinhline)""", re.X | re.DOTALL) 
    print(pattern.finditer(content)) 
    for m in pattern.finditer(content): 
     print ("in for loop") 
     print("Matched:\n----\n%s\n----\n" % m.group(2)) 
    print ("in split") 


def replacement(content): 
    split(content) 
    pattern = re.compile(r'(?<=\\\\\[-16pt]\n)([\s\S]*?)(?=\\\\\n\\thinhline)') 
    content= ' '.join(re.findall(pattern, content)) 
    print ("in replace") 
    return content 

這裏是輸出:

<callable-iterator object at 0x2ab2e09cfe10> 
in split 
in replace 

我已經嘗試了算法使用不同的字符串,它工作正常。我也測試過,看看內容是否是一個字符串,它是。即使進入split(),爲什麼程序不會進入for..loop?

謝謝。

+0

你想做什麼?我100%確定're.compile'中沒有錯誤。 – Maroun

+0

@MarounMaroun http://stackoverflow.com/questions/27745894/python-re-findall-how-to-separate-content-into-groups/27746133?noredirect=1#comment43910795_27746133這就是我想要做的,但有更大的一段文字。 – zara

+0

如果刪除'print(pattern.finditer(content))',會發生什麼? – MattDMo

回答

1

看評論:

def split(content): 
    pattern = re.compile(r"""(\\\[-16pt]\n)(.*?)(\n\\\nthinhline)""", re.X | re.DOTALL) 

    # the message you're seeing is correct - this line prints an iterator object - 
    # like all iterators, you must actually iterate over it to see the iterator's 
    # contents. You're seeing the string representation of an iterator, not the 
    # iterator's contents. 
    print(pattern.finditer(content)) 

    # this will iterate over the match objects in the iterator object - but there 
    # is no guarantee that any exist 
    for m in pattern.finditer(content): 
     print ("in for loop") 
     print("Matched:\n----\n%s\n----\n" % m.group(2)) 

    # now you're printing this string, which you correctly observed - note that it is 
    # outside of the for loop. This means that its execution is not dependent on the 
    # regex actually finding any matches. 
    print ("in split") 

由於「在循環」從來沒有打印,這意味着您正則表達式匹配從來沒有。我使用Python Regex Tool網站調試我的正則表達式取得了很好的成功。嘗試在一些示例文本上使用該網站,以確保您的正則表達式實際上符合您的期望。

你目前的問題是,你的正則表達式沒有找到任何匹配。

+0

我更喜歡使用http://pythex.org/ – mbomb007

+0

@ mbomb007 Oooh ...我還沒有看過那個網站。我也喜歡這個。 – skrrgwasme