<i id='ef4x3'><tr id='ef4x3'><dt id='ef4x3'><q id='ef4x3'><span id='ef4x3'><b id='ef4x3'><form id='ef4x3'><ins id='ef4x3'></ins><ul id='ef4x3'></ul><sub id='ef4x3'></sub></form><legend id='ef4x3'></legend><bdo id='ef4x3'><pre id='ef4x3'><center id='ef4x3'></center></pre></bdo></b><th id='ef4x3'></th></span></q></dt></tr></i><div id='ef4x3'><tfoot id='ef4x3'></tfoot><dl id='ef4x3'><fieldset id='ef4x3'></fieldset></dl></div>

        <small id='ef4x3'></small><noframes id='ef4x3'>

      1. <tfoot id='ef4x3'></tfoot>

          <bdo id='ef4x3'></bdo><ul id='ef4x3'></ul>

      2. <legend id='ef4x3'><style id='ef4x3'><dir id='ef4x3'><q id='ef4x3'></q></dir></style></legend>

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

        时间:2023-10-09

          • <small id='DXqpj'></small><noframes id='DXqpj'>

          • <tfoot id='DXqpj'></tfoot>

              <tbody id='DXqpj'></tbody>

                • <bdo id='DXqpj'></bdo><ul id='DXqpj'></ul>
                • <i id='DXqpj'><tr id='DXqpj'><dt id='DXqpj'><q id='DXqpj'><span id='DXqpj'><b id='DXqpj'><form id='DXqpj'><ins id='DXqpj'></ins><ul id='DXqpj'></ul><sub id='DXqpj'></sub></form><legend id='DXqpj'></legend><bdo id='DXqpj'><pre id='DXqpj'><center id='DXqpj'></center></pre></bdo></b><th id='DXqpj'></th></span></q></dt></tr></i><div id='DXqpj'><tfoot id='DXqpj'></tfoot><dl id='DXqpj'><fieldset id='DXqpj'></fieldset></dl></div>
                  <legend id='DXqpj'><style id='DXqpj'><dir id='DXqpj'><q id='DXqpj'></q></dir></style></legend>
                  本文介绍了Python 2.6+ str.format() 和正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  使用 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模板网!

                  上一篇:格式:在字符串中使用大括号时出现 KeyError 下一篇:如何使用 Python 验证特定的日期和时间格式

                  相关文章

                  最新文章

                  1. <i id='4KieT'><tr id='4KieT'><dt id='4KieT'><q id='4KieT'><span id='4KieT'><b id='4KieT'><form id='4KieT'><ins id='4KieT'></ins><ul id='4KieT'></ul><sub id='4KieT'></sub></form><legend id='4KieT'></legend><bdo id='4KieT'><pre id='4KieT'><center id='4KieT'></center></pre></bdo></b><th id='4KieT'></th></span></q></dt></tr></i><div id='4KieT'><tfoot id='4KieT'></tfoot><dl id='4KieT'><fieldset id='4KieT'></fieldset></dl></div>

                    <tfoot id='4KieT'></tfoot>
                    <legend id='4KieT'><style id='4KieT'><dir id='4KieT'><q id='4KieT'></q></dir></style></legend>
                      <bdo id='4KieT'></bdo><ul id='4KieT'></ul>

                    <small id='4KieT'></small><noframes id='4KieT'>