Python 2.6+ str.format() 和正则表达式
使用 str.format()
是 Python 2.6 和 Python 3 中格式化字符串的新标准。我在使用 str.format()
时遇到了问题正则表达式。
我编写了一个正则表达式来返回指定域以下一级的所有域或指定域以下 2 级的任何域,如果下面的第二级是 www...
假设指定的域是 Delivery.com ,我的正则表达式应该返回 a.delivery.com、b.delivery.com、www.c.delivery.com ...但它不应该返回 xadelivery.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!'
运行此命令应该给出结果:
String 1 matches!
String 3 matches!
现在,问题是当我尝试使用 str.format 动态替换delivery.com 时...
if (re.match('^(w{3}\.)?([0-9A-Za-z-]+\.){1}{domainName}$'.format(domainName = 'delivery.com'), str1): print 'String 1 matches!'
这似乎失败了,因为 str.format()
期望 < code>{3} 和 {1}
作为函数的参数。 (我假设)
我可以使用 + 运算符连接字符串
'^(w{3}\.)?([0-9A-Za-z-]+\.){1}' + domainName + '$'
问题归结为,当字符串(通常是正则表达式)具有“{”时是否可以使用 str.format()
n}”在里面?
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.
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...
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
Running this should give the result:
String 1 matches!
String 3 matches!
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}
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 + '
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
, str1): print 'String 1 matches!'
if (re.match('^(w{3}\.)?([0-9A-Za-z-]+\.){1}delivery.com
Running this should give the result:
Now, the problem is when I try to replace delivery.com dynamically using str.format...
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
, str2): print 'String 2 matches!'
if (re.match('^(w{3}\.)?([0-9A-Za-z-]+\.){1}delivery.com
Running this should give the result:
Now, the problem is when I try to replace delivery.com dynamically using str.format...
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
, str3): print 'String 3 matches!'
Running this should give the result:
Now, the problem is when I try to replace delivery.com dynamically using str.format...
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
.format(domainName = 'delivery.com'), str1): print 'String 1 matches!'
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
, str1): print 'String 1 matches!'
if (re.match('^(w{3}\.)?([0-9A-Za-z-]+\.){1}delivery.com
Running this should give the result:
Now, the problem is when I try to replace delivery.com dynamically using str.format...
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
, str2): print 'String 2 matches!'
if (re.match('^(w{3}\.)?([0-9A-Za-z-]+\.){1}delivery.com
Running this should give the result:
Now, the problem is when I try to replace delivery.com dynamically using str.format...
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
, str3): print 'String 3 matches!'
Running this should give the result:
Now, the problem is when I try to replace delivery.com dynamically using str.format...
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
Running this should give the result:
Now, the problem is when I try to replace delivery.com dynamically using str.format...
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
Running this should give the result:
Now, the problem is when I try to replace delivery.com dynamically using str.format...
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
Running this should give the result:
Now, the problem is when I try to replace delivery.com dynamically using str.format...
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
Running this should give the result:
Now, the problem is when I try to replace delivery.com dynamically using str.format...
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
Running this should give the result:
Now, the problem is when I try to replace delivery.com dynamically using str.format...
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
Running this should give the result:
Now, the problem is when I try to replace delivery.com dynamically using str.format...
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
The question comes down to, is it possible to use str.format()
when the string (usually regex) has "{n}" within it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您首先需要格式化字符串,然后使用正则表达式。将所有内容都放在一行中确实不值得。转义是通过双花括号来完成的:
此外,
re.match
在字符串的开头匹配,如果您使用则不必放置
,但是,如果您使用^
>re.matchre.search
,则需要^
。请注意,正则表达式中的
{1}
相当多余。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:
Also,
re.match
is matching at the beginning of the string, you don't have to put^
if you usere.match
, you need^
if you're usingre.search
, however.Please note, that
{1}
in regex is rather redundant.根据文档,如果您需要文字
{
或}
要在格式化操作中继续存在,请在原始字符串中使用{{
和}}
。Per the documentation, if you need a literal
{
or}
to survive the formatting opertation, use{{
and}}
in the original string.不幸的是,对于我的情况来说,它不能以这种方式工作,最后我使用了像这样的串联
pattern='{'+Acc[0]+'}(\.\d+)?'
其中 Acc[0] 是我的正则表达式中的变量项。我尝试了以下失败的格式。我与你分享也许你感兴趣(我使用python 3.7):
Unfortunately for my case it doesn't work in this way and finally I used concatenation like
pattern='{'+Acc[0]+'}(\.\d+)?'
where Acc[0] is the variable term in my regex.I tried following formats which are failed. I share it with you maybe you are interested (I use python 3.7):