Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# regex to validate "realistic" IP values

Tags:

c#

.net

regex

Regex that somewhat validates if a value has one of the following characteristics:

123-29-123-123.subdomain.zomg.com:8085
123.12.34.56:420

Unfortunately, I'm terrible at Regex, C#, google searches, and the differences between proper nouns and regular ones.

It can be a lose approximation, in fact I would go with anything that has a : colon separator with a port after it.

like image 265
y2k Avatar asked Dec 06 '25 04:12

y2k


1 Answers

Will this work?

^(?<Host>[^:]+)(?::(?<Port>\d+))?$

This gives me:

Host = 123-29-123-123.subdomain.zomg.com
Port = 8085

and

Host = 123.12.34.56:420
Port = 420
like image 110
John Gietzen Avatar answered Dec 08 '25 16:12

John Gietzen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!