Python 2.6+ str.format() 和正则表达式

发布于 2024-08-14 02:26:49 字数 1290 浏览 8 评论 0原文

使用 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?

, 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花开浅夏 2024-08-21 02:26:49

您首先需要格式化字符串,然后使用正则表达式。将所有内容都放在一行中确实不值得。转义是通过双花括号来完成的:

>>> pat= '^(w{{3}}\.)?([0-9A-Za-z-]+\.){{1}}{domainName}

此外,re.match在字符串的开头匹配,如果您使用则不必放置^ >re.match,但是,如果您使用 re.search,则需要 ^

请注意,正则表达式中的 {1} 相当多余。

.format(domainName = 'delivery.com') >>> pat '^(w{3}\\.)?([0-9A-Za-z-]+\\.){1}delivery.com

此外,re.match在字符串的开头匹配,如果您使用则不必放置^ >re.match,但是,如果您使用 re.search,则需要 ^

请注意,正则表达式中的 {1} 相当多余。

>>> re.match(pat, str1)

此外,re.match在字符串的开头匹配,如果您使用则不必放置^ >re.match,但是,如果您使用 re.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:

>>> pat= '^(w{{3}}\.)?([0-9A-Za-z-]+\.){{1}}{domainName}

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.

Please note, that {1} in regex is rather redundant.

.format(domainName = 'delivery.com') >>> pat '^(w{3}\\.)?([0-9A-Za-z-]+\\.){1}delivery.com

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.

Please note, that {1} in regex is rather redundant.

>>> re.match(pat, str1)

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.

Please note, that {1} in regex is rather redundant.

一向肩并 2024-08-21 02:26:49

根据文档,如果您需要文字{} 要在格式化操作中继续存在,请在原始字符串中使用 {{}}

'^(w{{3}}\.)?([0-9A-Za-z-]+\.){{1}}{domainName}
.format(domainName = 'delivery.com')

Per the documentation, if you need a literal { or } to survive the formatting opertation, use {{ and }} in the original string.

'^(w{{3}}\.)?([0-9A-Za-z-]+\.){{1}}{domainName}
.format(domainName = 'delivery.com')
野稚 2024-08-21 02:26:49

不幸的是,对于我的情况来说,它不能以这种方式工作,最后我使用了像这样的串联
pattern='{'+Acc[0]+'}(\.\d+)?' 其中 Acc[0] 是我的正则表达式中的变量项。

我尝试了以下失败的格式。我与你分享也许你感兴趣(我使用python 3.7):

pattern='{{Acc[0]}}(\.\d+)?'
pattern='{{ID}}(\.\d+)?'.format(ID = Acc[0])
pattern='{{1}}(\.\d+)?'.format(ID = Acc[0])
pattern="{{}}(\.\d+)?".format(Acc[0])
pattern=fr"{{Acc[0]}}(\.\d+)?"
pattern = "%s(\\.\d+)?" % (Acc[0])

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

pattern='{{Acc[0]}}(\.\d+)?'
pattern='{{ID}}(\.\d+)?'.format(ID = Acc[0])
pattern='{{1}}(\.\d+)?'.format(ID = Acc[0])
pattern="{{}}(\.\d+)?".format(Acc[0])
pattern=fr"{{Acc[0]}}(\.\d+)?"
pattern = "%s(\\.\d+)?" % (Acc[0])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文