[英]I'm reading the book the self taught programmer ch17 and I can't figure out why this dosn't work
匹配多行
import re
zen = """Although never is often better than *right* now. If the implementation
is hard to explain, it's a bad idea. If the implementation is easy to explain, it
may be a good idea. Namespaces are one honking great idea -- let's do more of those!
m = re.findall("^If", zen, re.MULTILINE)
print(m)
[if,if]
是應該打印的,但我得到的只是[]
您沒有正確復制示例。 當您使用re.MULTILINE
標志時, ^
匹配行的開頭,因此這只匹配If
當它位於行的開頭時。 我沒有這本書,但我認為它的每個句子都有自己的一行。
import re
zen = """Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!"""
m = re.findall("^If", zen, re.MULTILINE)
print(m)
脫字符^
表示If
必須位於行首。 刪除它,你會得到你想要的答案。
import re
zen = """Although never is often better than *right* now. If the implementation
is hard to explain, it's a bad idea. If the implementation is easy to explain, it
may be a good idea. Namespaces are one honking great idea -- let's do more of those! """
m = re.findall("If", zen, re.MULTILINE)
print(m)
Python 的實際禪宗每行有一個句子:
zen = """
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!"""
m = re.findall("^If", zen, re.MULTILINE)
print(m)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.