Question

How would I modify this IPv6 regex I wrote to either detect the address (ie the way the regex is written right now), but also accept "blank" ie the user did not specify an IPv6 address?

^[0-9a-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}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}$

Right now, the regex is looking for a minimum of 0:0:0:0:0:0:0:0 or similar. Infact in addition to a blank address, I probably need to also be able to handle compression such as the following address:

FE80::1
or ::1
etc

Thanks!

* UPDATE *

So let me make sure I have this straight...

(^$|^IPV4)\|(^$|IPV6)\|REST OF STUFF$

That doesn't seem right. I feel like I have misplaced the ^ and $ and the very beginning and end of my entire regex.

Maybe this instead:

 ^(^$|IPV4)\|(^$|IPV6)\|REST$

* UPDATE *

Still no luck. Here is part of my code with the middles chopped out for sanity:

^(|[0-9]{1,3}.<<<OMIT MIDDLE IPV4>>>.[0-9]{1,3})\|(|(\A([0-9a-f]{1,4}:){1,1}<<<OMIT MIDDLE IPV6>>>[0-1]?\d?\d)){3}\Z))\|[a-zA-Z0-<<<MORE STUFF MIDDLE OMITTED>>>{0,50}$

I hope that isn't confusing. Thats the beginning and end of each regex with the middles omitted so you can see the ( ).

Perhaps I need to enclose the entire gigantic IPV6 regex in parenthesis?

* UPDATE *

Tried last statement above... no luck.

Was it helpful?

Solution

You can specify alternation with the | character, so a|b means "match either a or b". In this case it would look something like this:

^$|^[0-9a-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}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}:[0-9a-fA-F]{1,4}$

The regex ^$ will match empty strings, so ^$|<current-regex> means "match either an empty string, or whatever <current-regex> matches (in this case IPv6)". You could use ^\s*$ in place of ^$ if you want strings that only consist of whitespace character to also be considered "empty".

This just handles the first part of the question, handling the compression like FE80::1 is more complex and it looks like there are already some other good answers for that in comments (note that I don't think this question is a dupe, because the "also matching an empty string" part isn't present in those questions).

edit: If it is part of a larger regex, then you should wrap everything in a group and get rid of the ^$, so it would be something like (|<current-regex>). Since there is nothing before the |, it means that the group can match either empty strings or whatever your current regex would match.

OTHER TIPS

According to this post on this site called Stack Overflow this other site has an explanation & example of a huge—but very usable—regex which is this:

(\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6}\Z)|
(\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}\Z)|
(\A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}\Z)|
(\A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}\Z)|
(\A([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}\Z)|
(\A([0-9a-f]{1,4}:){1,6}(:[0-9a-f]{1,4}){1,1}\Z)|
(\A(([0-9a-f]{1,4}:){1,7}|:):\Z)|
(\A:(:[0-9a-f]{1,4}){1,7}\Z)|
(\A((([0-9a-f]{1,4}:){6})(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})\Z)|
(\A(([0-9a-f]{1,4}:){5}[0-9a-f]{1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3})\Z)|
(\A([0-9a-f]{1,4}:){5}:[0-9a-f]{1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|
(\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,4}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)(\.(25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}\Z)|
(\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,3}:(25[0-5]|2[0-4]\d|[0-1]?\d?\d)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