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 matches servername

  • (:(?<port>.+))? matches followed :. part matches portnumber

    • ? quantifier, quantifies 0 or 1 occurence of portnumber

Comments

Popular posts from this blog

matlab - "Contour not rendered for non-finite ZData" -

delphi - Indy UDP Read Contents of Adata -

javascript - Any ideas when Firefox is likely to implement lengthAdjust and textLength? -