Python Regular Expression for SIP URI variables?

和自甴很熟 提交于 2019-12-01 06:03:52

There are no headers and params in your example, so I dont know how they show up. But you can use the following code to match your example strings:

[EDIT1 - Added regex to match hostname strings and support for user:password, based on OPs new example URIs]

[EDIT2 - Added the params and headers regex and commented more on the 'OR' part of the regex]

import re

uriList = [
        'sip:192.1.2.3',
        'sip:123@192.1.2.3',
        'sip:192.1.2.3:5060',
        'sip:123@[2620:0:2ef0:7070:250:60ff:fe03:32b7]',
        'sip:[2620:0:2ef0:7070:250:60ff:fe03:32b7]',
        'sip:[2620:0:2ef0:7070:250:60ff:fe03:32b7]:5060',
        'sips:support@voip.example.com',
        'sip:22444032@voip.example.com:6000',
        'sip:support:pass@212.123.1.213',
        'sip:support:pass@212.123.1.213;urlparams=test',
        'sip:support:pass@212.123.1.213?auth=basic',
        'sip:support:pass@212.123.1.213;urlparams=test?auth=basic',
        ]

mPattern = re.compile(
                        '(?P<scheme>\w+):' #Scheme
                        +'(?:(?P<user>[\w\.]+):?(?P<password>[\w\.]+)?@)?' #User:Password
                        +'\[?(?P<host>' #Begin group host
                            +'(?:\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|' #IPv4 address Host Or
                            +'(?:(?:[0-9a-fA-F]{1,4}):){7}[0-9a-fA-F]{1,4}|' #IPv6 address Host Or
                            +'(?:(?:[0-9A-Za-z]+\.)+[0-9A-Za-z]+)'#Hostname string
                        +')\]?:?' #End group host
                        +'(?P<port>\d{1,6})?' #port
                        +'(?:\;(?P<params>[^\?]*))?' # parameters
                        +'(?:\?(?P<headers>.*))?' # headers
                        )
groupNamesList = ['scheme', 'user', 'password', 'host', 'port', 'params', 'headers'] #List of group Names

for uri in uriList: #iterate through the list of uri
    mObject = mPattern.search(uri) #pattern search
    if mObject: #if you find a match
        groupStrings = [mObject.group(groupName) if mObject.group(groupName) else '' for groupName in groupNamesList] #extract your groupStrings
        print('Scheme: {0}, User: {1}, Password: {2}, Host: {3}, Port: {4}, Params: {5}, Headers: {6}'.format(*groupStrings)) #print groupStrings

The Output I get it:

Scheme: sip, User: , Password: , Host: 192.1.2.3, Port: , Params: , Headers: 
Scheme: sip, User: 123, Password: , Host: 192.1.2.3, Port: , Params: , Headers: 
Scheme: sip, User: , Password: , Host: 192.1.2.3, Port: 5060, Params: , Headers: 
Scheme: sip, User: 123, Password: , Host: 2620:0:2ef0:7070:250:60ff:fe03:32b7, Port: , Params: , Headers: 
Scheme: sip, User: , Password: , Host: 2620:0:2ef0:7070:250:60ff:fe03:32b7, Port: , Params: , Headers: 
Scheme: sip, User: , Password: , Host: 2620:0:2ef0:7070:250:60ff:fe03:32b7, Port: 5060, Params: , Headers: 
Scheme: sips, User: support, Password: , Host: voip.example.com, Port: , Params: , Headers: 
Scheme: sip, User: 22444032, Password: , Host: voip.example.com, Port: 6000, Params: , Headers: 
Scheme: sip, User: support, Password: pass, Host: 212.123.1.213, Port: , Params: , Headers: 
Scheme: sip, User: support, Password: pass, Host: 212.123.1.213, Port: , Params: urlparams=test, Headers: 
Scheme: sip, User: support, Password: pass, Host: 212.123.1.213, Port: , Params: , Headers: auth=basic
Scheme: sip, User: support, Password: pass, Host: 212.123.1.213, Port: , Params: urlparams=test, Headers: auth=basic

Try this out and see if it works for you

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