2012-03-30 67 views
15

我有一个字符串,我想在一开始结尾匹配一个搜索模式。如何才能做到这一点?如何在Python的正则表达式中匹配开始和结束?

比方说,我们有一个字符串,如:

string = "ftp://www.somewhere.com/over/the/rainbow/image.jpg" 

我想要做这样的事情:

re.search("^ftp:// & .jpg$" ,string) 

显然,这是不正确的,但我希望它横跨得到我的观点。这可能吗?

+1

你认为要检查文档吗? – Marcin 2012-03-30 16:42:02

回答

13

re.matchmatch the string at the beginning,而相比之下,re.search

re.match(r'(ftp|http)://.*\.(jpg|png)$', s) 

有两点需要注意这里:

  • r''用于字符串字面意思是在正则表达式中加入反斜杠
  • string是一个标准的模块,所以我选择了s作为变量
  • 如果使用正则表达式超过一次,你可以使用r = re.compile(...)内置状态机一次,然后用r.match(s)事后匹配字符串

如果你愿意,你也可以使用urlparse模块解析URL为你(虽然你仍然需要提取扩展名):

>>> allowed_schemes = ('http', 'ftp') 
>>> allowed_exts = ('png', 'jpg') 
>>> from urlparse import urlparse 
>>> url = urlparse("ftp://www.somewhere.com/over/the/rainbow/image.jpg") 
>>> url.scheme in allowed_schemes 
True 
>>> url.path.rsplit('.', 1)[1] in allowed_exts 
True 
15

如何根本不使用正则表达式?

if string.startswith("ftp://") and string.endswith(".jpg"): 

你不觉得这个更好吗?

还可以支持开始和结束多种选择:

if (string.startswith(("ftp://", "http://")) and 
    string.endswith((".jpg", ".png"))): 
+0

我会,但它更复杂,因为有一些有效的开始和结束序列。如果我知道如何做这个简单的例子,我可以使它适应更复杂的现实。 :) – 2012-03-30 16:40:44

+1

@Google:你也可以查询多个字符串,看看我的更新。 – 2012-03-30 16:42:54

3

尝试

re.search(r'^ftp://.*\.jpg$' ,string) 

如果你想有一个正则表达式搜索。请注意,您必须跳过这段时间,因为它在正则表达式中有特殊含义。

9

不要贪心,使用^ftp://(.*?)\.jpg$

2
import re 

s = "ftp://www.somewhere.com/over/the/rainbow/image.jpg" 
print(re.search("^ftp://.*\.jpg$", s).group(0)) 
相关问题