Regex IPv6 Validation and optional square brackets?

Hi all regex newbie here.,

I am trying to write a regular expression that validates IPv6 addresses. I have not added a part of the port yet, I want to process part of the address first.

This is what I still have:

\\[?(?:[:0-9A-Fa-f]+)\\]?

This makes the optional opening and closing brackets. But, as you see, they are independent. Is there a way with regular expressions to require both opening and closing brackets or not?

+3
source share
2 answers

Regex is a great tool for matching strings, but you hit one of its weaknesses. Matching pairs of brackets can be tricky.

- - , .

, , , , , . , , , .

, : . , , , - or:

\(\[(?:[:0-9A-Fa-f]+)\])|(?:[:0-9A-Fa-f]+)\

, .

+3

, ... : ^ ([0-9a-fA-F] +:) {7} [0-9a-fA-F] + $ : ^ [0-9a-fA-F] {1,4}: [0-9a-fA-F] {1,4}: [0-9a-fA-F] {1,4}: [0- 9-FA-F] {1,4}: [0-9a-FA-F] {1,4}: [0-9a-FA-F] {1,4}: [0-9a-FA-F ] {1,4}: [0-9a-FA-F] {1,4} $

-1

All Articles