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

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

问题描述

Python re 模块?

我已阅读文档(当前文档),但我好像从来不记得.我一直不得不查找它并重新学习它.我希望有人能用例子清楚地回答它,这样(也许)它会留在我的脑海中.或者至少我会有一个更好的地方来回答我的问题,并且重新学习它会花费更少的时间.

解决方案

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

正如 re.match 文档 所说:><块引用>

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

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

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

<块引用>

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

所以如果你需要匹配字符串的开头,或者匹配整个字符串使用match.它更快.否则使用 search.

文档有一个 特定部分用于 match vs. search 也包括多行字符串:

<块引用>

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

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

现在,说够了.是时候看看一些示例代码了:

# 示例代码:string_with_newlines = """something别的东西"""进口重新print re.match('some', string_with_newlines) # 匹配打印 re.match('someother',string_with_newlines) # 不匹配打印 re.match('^someother', string_with_newlines,re.MULTILINE) # 也不匹配打印研究('某人',string_with_newlines) # 找到一些东西打印 re.search('^someother', string_with_newlines,re.MULTILINE) # 也找到了一些东西m = re.compile('东西$', re.MULTILINE)打印 m.match(string_with_newlines) # 不匹配打印 m.match(string_with_newlines, pos=4) # 匹配打印 m.search(string_with_newlines,re.MULTILINE) # 也匹配

What is the difference between the search() and match() functions in the Python re module?

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 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.

As the re.match documentation says:

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.

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

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

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.

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.

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

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).

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天全站免登陆