看來re.match和re.search w /'^'是一樣的,除了re.search 可以使用re.MULTILINE標誌使它更加靈活。'^'的re.match和re.search如何不同?
string ="""U.S. stock-index futures pointed
to a solidly higher open on Monday
North Korea. That issue overshadowed the state of
the equity market, where earnings
have been strong at a time of high
employment and low inflation,
as well as valuations that a
ppear elevated by many metrics, north korea"""
import re
re.search('^North Korea\.?', string) # no match
re.match('^North Korea\.?', string) # no match
re.search('^North Korea\.?', string, flags = re.MULTILINE).group() #match
使用一個比另一個有任何好處?只是剛剛開始
>>> re.match("c", "abcdef") # No match
>>> re.search("c", "abcdef") # Match
<_sre.SRE_Match object at ...>
re.search()檢查匹配的字符串中的任何
你爲什麼認爲他們做*或*應該*有區別? –
查看[文檔]他們是非常不同的,因爲'match'只看字符串的開始,而'search'看起來貫穿整個字符串 –
@Professor_Joykill我在談論使用re.search與' ^'標誌。 – Moondra