Skip to content

Instantly share code, notes, and snippets.

@ggrandes
Created March 27, 2022 20:29
Show Gist options
  • Save ggrandes/cd694c50508dba5e81bd0778a9cc2a41 to your computer and use it in GitHub Desktop.
Save ggrandes/cd694c50508dba5e81bd0778a9cc2a41 to your computer and use it in GitHub Desktop.
Regular Expression - IPv4 (Perl / Java)
Match: 0.0.0.0-255.255.255.255

Perl / PCRE

$input =~ /^(?:(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(?1)$/;
grep -P '^(?:(25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)\.){3}(?1)$' <<<"$input"

Java

Pattern.compile("^(?:(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)\\.){3}(?:25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)$")
       .matcher(input).matches();

References:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment