isIpv4 property

bool? isIpv4

Checks whether the String is valid IPv4.

Example 1

String foo = '192.168.1.14';
bool isIpv4 = foo.isIpv4; // returns true

Example 2

String foo = '192.168.1.14.150.1225';
bool isIpv4 = foo.isIpv4; // returns false

Implementation

bool? get isIpv4 {
  if (this == null) return null;
  if (this!.isEmpty) return false;
  final regex = RegExp(
    r'((?:^|\s)([a-z]{3,6}(?=://))?(://)?((?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?))(?::(\d{2,5}))?(?:\s|$))',
  );
  return regex.hasMatch(this!);
}