Python URI变量的正则表达式? [解决了] [英] Python Regular Expression for SIP URI variables? [SOLVED]

查看:140
本文介绍了Python URI变量的正则表达式? [解决了]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  _syntax = re.compile('^(?P< scheme> [a-zA-Z] [a-zA-Z0-9\ + \-\。] *):'#scheme 
+' ????:( :( P<使用者> [A-ZA-Z0-9\-\_\.\ \〜\ * \'\(\)及!= \\ \\?\ $ ,; \?\ / \%] +)'#user
+'(?::(?P< password> [^:@; \?] +)) ?)@)?'#password
+'(?:(?:(?P< host> [^; \?:] *)(?: :(?P< port> [\d ]?'#host,port
+'(?:;(?P< params> [^ \?] *))?'#参数
+'(? \?(?P< headers>。*))?$')#headers

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

我需要修改此表达式以支持IPv6并匹配所有t他不同类型的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',
]

输出



方案:sip,用户:123,主机:192.1.2.3,端口:
方案:sip,用户:,主机:192.1.2.3,端口:5060
方案:sips,用户:123,主机:2620:0:2ef0:707方案:sip,用户:,主机:2620:0:2ef0:7070:250:60ff:fe03:32b7,端口:
方案:sip ,用户:,主机:2620:0:2ef0:7070:250:60ff:fe03:32b7,端口:5060
方案:sips,用户:支持,主机:voip.example.com
方案: sip,用户:22444032,主机:voip.example.com,端口:6000
方案:sip,用户:thks.ashwin,密码:pass,主机:212.123.1.213

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



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

解决方案

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





  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\。] +)?@)?'密码
+'\ [?(?P< host>'#Begin组主机
+'(?: \d {1,3} \.\d {1,3} \.\d {1,3} \.\d {1,3})|'#IPv4地址主机或
+'(?:(?:[0-9a-fA-F ] {{}} {[{}} {} {(0)(1) Za-z] + \。)+ [0-9A-Za-z] +)'#主机名字符串
+')\]?:?'#End组主机
+'( ?P< port> \d {1,6})?'#port
+'(?: \;(?P< params> [^ \?] *))?'#参数
+'(?: \?(?P< headers>。*))? '#headers

groupNamesList = ['scheme','user','password','host','port','params','headers']#
$ b在uriList中的uri:#iterate通过uri
的列表mObject = mPattern.search(uri)#pattern搜索
如果mObject:#如果找到匹配
groupStrings = [mObject.group(groupName)if mObject.group(groupName)else''for groupName in groupNamesList] #extract your groupStrings
print('Scheme:{0},User:{1},Password:{ 2},主机:{3},端口:{4},参数:{5},标题:{6}'。format(* groupStrings))#print groupStrings
pre>

输出我得到它:

  Scheme: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,用户:支持,密码:,主机:voip.example.com,端口:,参数:,标题:
方案:sip,用户:22444032,密码:,主机:voip.example.com,端口:6000,参数:,标题:
方案:sip,用户:支持,密码:pass,主机:212.123.1.213 ,端口:,参数:,标题:
方案:sip,用户:支持,密码:通过,主机:212.123.1.213,端口:,参数:urlparams =测试,标题:
方案:用户:支持,密码:通过,主机:212.123.1.213,端口:,参数:,标题:auth = basic
方案:sip,用户:支持,密码:pass,主机:212.123.1.213,端口:参数:urlparams = test,标题:auth = basic

尝试这出来看看它是否适合你


I am using this regular expression for SIP (Session Initiation Protocol) URIs to extract the different internal variables.

_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()

I need to modify this expression to support IPv6 and match all the different types of SIP URIs. The basic idea is that IPv4 shows the form 192.168.0.1 and IPv6 2620:0:2ef0:7070:250:60ff:fe03:32b7. Beacause the port number is after :, the IPv6 is between brakets in the SIP URI.

Its general form is:

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

These are some examples:

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

Output

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

I tried to modify the host expression to match both [IPv6] and IPv4 expression but without luck =´(

I've been using https://pythex.org/ to test the results.

解决方案

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

这篇关于Python URI变量的正则表达式? [解决了]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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