powershell - Regex optionally match port number in url -
i have regex http:\/\/(?<servername>[^"]+):(?<port>[^"]+)
pick server , port url. works fine when port exists in url when port number doesn't exist doesn't match server either. example,
url -> http://server1:20 values returned servername = server1 port = 20 url -> http://server2 values returned servername = blank port = blank
for second case values servername = server2 port = blank
you can modify regex bit as
^http:\/\/(?<servername>[^:]+)(:(?<port>.+))?$
example : http://regex101.com/r/sn8it4/1
changes made
[^:]+
matches other:
. part matchesservername
(:(?<port>.+))?
matches followed:
. part matchesportnumber
?
quantifier, quantifies 0 or 1 occurence of portnumber
Comments
Post a Comment