re.search和re.match有什么区别? [英] What is the difference between re.search and re.match?

查看:139
本文介绍了re.search和re.match有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python re模块?

我已经阅读了文档(当前文档),但我似乎永远不会记住它.我一直在查找并重新学习它.我希望有人会用示例清楚地回答它,以便(也许)它会出现在我的脑海中.或者至少我将有一个更好的地方回答我的问题,并且重新学习它的时间会更少.

I've read the documentation (current documentation), but I never seem to remember it. I keep having to look it up and re-learn it. I'm hoping that someone will answer it clearly with examples so that (perhaps) it will stick in my head. Or at least I'll have a better place to return with my question and it will take less time to re-learn it.

推荐答案

re.match锚定在字符串的开头.这与换行符无关,因此与在模式中使用^不同.

re.match is anchored at the beginning of the string. That has nothing to do with newlines, so it is not the same as using ^ in the pattern.

re.match文档说:

如果在处的零个或多个字符 字符串开头与正则表达式模式匹配,返回 相应的MatchObject实例. 如果字符串不返回None 匹配模式请注意,这是 与零长度匹配不同.

If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding MatchObject instance. Return None if the string does not match the pattern; note that this is different from a zero-length match.

注意:如果要查找匹配项 在字符串中的任何地方,使用search() 代替.

Note: If you want to locate a match anywhere in string, use search() instead.

re.search搜索整个字符串,如文档所述 :

re.search searches the entire string, as the documentation says:

扫描字符串以查找 正则表达式所在的位置 模式产生一个匹配,并返回一个 相应的MatchObject实例. 如果None在 字符串与模式匹配;注意 这不同于找到一个 零长度匹配 字符串.

Scan through string looking for a location where the regular expression pattern produces a match, and return a corresponding MatchObject instance. Return None if no position in the string matches the pattern; note that this is different from finding a zero-length match at some point in the string.

因此,如果您需要匹配字符串的开头,或者匹配整个字符串,请使用match.它更快.否则请使用search.

So if you need to match at the beginning of the string, or to match the entire string use match. It is faster. Otherwise use search.

该文档的match与.c相比,有特定部分. search 还涵盖了多行字符串:

The documentation has a specific section for match vs. search that also covers multiline strings:

Python提供了两种不同的原语 基于常规的操作 表达式:match检查是否匹配 仅在字符串的开头, 而search检查匹配项 字符串中的任何地方(这就是 Perl会默认执行此操作.

Python offers two different primitive operations based on regular expressions: match checks for a match only at the beginning of the string, while search checks for a match anywhere in the string (this is what Perl does by default).

请注意,match可能与search不同 即使使用正则表达式 以'^'开头:'^'仅匹配 在字符串的开头或 MULTILINE模式也立即 跟随换行符. "match" 仅当模式时,操作成功 在字符串的开始处匹配 无论模式如何,还是在开始时 可选pos给出的位置 不管是否一个参数 换行符在它之前.

Note that match may differ from search even when using a regular expression beginning with '^': '^' matches only at the start of the string, or in MULTILINE mode also immediately following a newline. The "match" operation succeeds only if the pattern matches at the start of the string regardless of mode, or at the starting position given by the optional pos argument regardless of whether a newline precedes it.

现在,足够多的谈话.是时候看一些示例代码了:

Now, enough talk. Time to see some example code:

# example code:
string_with_newlines = """something
someotherthing"""

import re

print re.match('some', string_with_newlines) # matches
print re.match('someother', 
               string_with_newlines) # won't match
print re.match('^someother', string_with_newlines, 
               re.MULTILINE) # also won't match
print re.search('someother', 
                string_with_newlines) # finds something
print re.search('^someother', string_with_newlines, 
                re.MULTILINE) # also finds something

m = re.compile('thing$', re.MULTILINE)

print m.match(string_with_newlines) # no match
print m.match(string_with_newlines, pos=4) # matches
print m.search(string_with_newlines, 
               re.MULTILINE) # also matches

这篇关于re.search和re.match有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆