将正则表达式分为2个捕获组 [英] Split regular expression into 2 capture groups

查看:89
本文介绍了将正则表达式分为2个捕获组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我以前的问题都得到了回答...我还有一个,对我来说这更困难...

Ok, my previous questions were answered ... I have one more, this one is more difficult for me...

^([A-Za-z]+\.[A-Za-z0-9_-]+\.domain\.com)$

现在,此表达式仅产生1个捕获组,如^()所示;我将如何为该URL设置2个捕获组? (用于IIS正则表达式重写)

Right now this expression produces only 1 capture group as noted with ^() ; How would I do 2 capture groups for this URL? (for IIS regular expression rewrite)

推荐答案

您可以通过将相关部分括起来来做到这一点:

You can do this by enclosing the relevant portions like so:

text    = "city.state.domain.com"
pattern = "^([^\.]+).([^\.]+).([^\.]+).([^\.]+)$"
match   = re.match(pattern, text)
match.groups()
# Returns: [ 'city', 'state', 'domain', 'com' ]

这篇关于将正则表达式分为2个捕获组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆