小编典典

SIP URI变量的Python正则表达式?

python

我将此正则表达式用于SIP(会话初始化协议)URI,以提取不同的内部变量。

_syntax = re.compile('^(?P<scheme>[a-zA-Z][a-zA-Z0-9\+\-\.]*):'  # scheme
        + '(?:(?:(?P<user>[a-zA-Z0-9\-\_\.\!\~\*\'\(\)&=\+\$,;\?\/\%]+)' # user
        + '(?::(?P<password>[^:@;\?]+))?)@)?' # password
        + '(?:(?:(?P<host>[^;\?:]*)(?::(?P<port>[\d]+))?))'  # host, port
        + '(?:;(?P<params>[^\?]*))?' # parameters
        + '(?:\?(?P<headers>.*))?$') # headers

m = URI._syntax.match(value)
    if m: 
        self.scheme, self.user, self.password, self.host, self.port, params, headers = m.groups()

我需要修改此表达式以支持IPv6并匹配所有不同类型的SIP URI。基本思想是IPv4显示的格式为192.168.0.1和IPv6
2620:0:2ef0:7070:250:60ff:fe03:32b7。因为端口号在:之后,所以IPv6在SIP URI的中间。

其一般形式为:

sip:user:password @ host:port; uri-parameters?headers

这些是一些示例:

uriList = [
   'sip:192.1.2.3',
   'sip:123@192.1.2.3',
   'sip:192.1.2.3:5060',
   'sips: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:thks.ashwin:pass@212.123.1.213',
   ]

输出量

Scheme: sip, User: , Host: 192.1.2.3, Port: 
Scheme: sip, User: 123, Host: 192.1.2.3, Port: 
Scheme: sip, User: , Host: 192.1.2.3, Port: 5060
Scheme: sips, User: 123, Host: 2620:0:2ef0:7070:250:60ff:fe03:32b7, Port: 
Scheme: sip, User: , Host: 2620:0:2ef0:7070:250:60ff:fe03:32b7, Port: 
Scheme: sip, User: , Host: 2620:0:2ef0:7070:250:60ff:fe03:32b7, Port: 5060
Scheme: sips, User:support , Host: voip.example.com
Scheme: sip, User:22444032 , Host: voip.example.com, Port: 6000
Scheme: sip, User:thks.ashwin, Password:pass ,Host: 212.123.1.213

我试图修改主机表达式以匹配[IPv6]和IPv4表达式,但是没有运气=´(

我一直在使用https://pythex.org/来测试结果。


阅读 212

收藏
2021-01-20

共1个答案

小编典典

您的示例中没有标题和参数,因此我不知道它们如何显示。但是您可以使用以下代码来匹配示例字符串:

[EDIT1-根据OP的新示例URI添加了正则表达式以匹配主机名字符串和对user:password的支持]

[EDIT2-添加了参数和标头正则表达式,并在正则表达式的“或”部分添加了更多注释]

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

我得到的输出:

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

试试看,看看是否适合您

2021-01-20