問題描述
使用 str.format()
是 Python 2.6 和 Python 3 中格式化字符串的新標準.使用 str.format()
時遇到問題code> 帶有正則表達式.
Using str.format()
is the new standard for formatting strings in Python 2.6, and Python 3. I've run into an issue when using str.format()
with regular expressions.
我編寫了一個正則表達式來返回比指定域低一級的所有域或比指定域低兩級的任何域,如果下面的第二級是 www...
I've written a regular expression to return all domains that are a single level below a specified domain or any domains that are 2 levels below the domain specified, if the 2nd level below is www...
假設指定的域是delivery.com,我的正則表達式應該返回a.delivery.com、b.delivery.com、www.c.delivery.com ...但它不應該返回xadelivery.com.
Assuming the specified domain is delivery.com, my regex should return a.delivery.com, b.delivery.com, www.c.delivery.com ... but it should not return x.a.delivery.com.
import re
str1 = "www.pizza.delivery.com"
str2 = "w.pizza.delivery.com"
str3 = "pizza.delivery.com"
if (re.match('^(w{3}.)?([0-9A-Za-z-]+.){1}delivery.com$', str1): print 'String 1 matches!'
if (re.match('^(w{3}.)?([0-9A-Za-z-]+.){1}delivery.com$', str2): print 'String 2 matches!'
if (re.match('^(w{3}.)?([0-9A-Za-z-]+.){1}delivery.com$', str3): print 'String 3 matches!'
運行它應該會給出結果:
Running this should give the result:
String 1 matches!
String 3 matches!
現在,問題是當我嘗試使用 str.format 動態替換 delivery.com...
Now, the problem is when I try to replace delivery.com dynamically using str.format...
if (re.match('^(w{3}.)?([0-9A-Za-z-]+.){1}{domainName}$'.format(domainName = 'delivery.com'), str1): print 'String 1 matches!'
這似乎失敗了,因為 str.format()
期望 {3}
和 {1}
是功能.(我假設)
This seems to fail, because the str.format()
expects the {3}
and {1}
to be parameters to the function. (I'm assuming)
我可以使用 + 運算符連接字符串
I could concatenate the string using + operator
'^(w{3}.)?([0-9A-Za-z-]+.){1}' + domainName + '$'
問題歸結為,當字符串(通常是正則表達式)中包含{n}"時是否可以使用str.format()
?
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
推薦答案
您首先需要格式化字符串,然后使用正則表達式.將所有內容放在一行中確實不值得.轉義是通過加倍花括號來完成的:
you first would need to format string and then use regex. It really doesn't worth it to put everything into a single line. Escaping is done by doubling the curly braces:
>>> pat= '^(w{{3}}.)?([0-9A-Za-z-]+.){{1}}{domainName}$'.format(domainName = 'delivery.com')
>>> pat
'^(w{3}\.)?([0-9A-Za-z-]+\.){1}delivery.com$'
>>> re.match(pat, str1)
另外,re.match
在字符串的開頭匹配,如果使用re.match
就不用放^
code>,但是如果您使用 re.search
,則需要 ^
.
Also, re.match
is matching at the beginning of the string, you don't have to put ^
if you use re.match
, you need ^
if you're using re.search
, however.
請注意,正則表達式中的 {1}
相當多余.
Please note, that {1}
in regex is rather redundant.
這篇關于Python 2.6+ str.format() 和正則表達式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!