Split regular expression into 2 capture groups

杀马特。学长 韩版系。学妹 提交于 2020-01-02 08:43:21

问题


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

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)


回答1:


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' ]


来源:https://stackoverflow.com/questions/9069938/split-regular-expression-into-2-capture-groups

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!