parseElectricityRef function

ElectricityRef? parseElectricityRef(
  1. String input
)

Parses a Pakistani electricity bill identifier: a DISCO reference number, a DISCO customer ID or a K-Electric account number.

Formats are those the utilities' own bill pages ask for (retrieved 2026-11-18; see DATA.md in the repository): PITC's bill pages take a 14-digit reference number with a separate U/R choice, or a 10-digit customer ID (11 digits for LESCO); K-Electric asks for a 13-digit account number starting with 04000. No check digit is published, so only the format is checked.

Spaces, dashes and dots are ignored, and Urdu digits are accepted.

Returns null if the input matches none of the formats. Never throws.

parseElectricityRef('02-12624-0045901 R')?.formatted;
// '02 12624 0045901 R'
parseElectricityRef('0400012345678')?.disco; // 'K-Electric'

Implementation

ElectricityRef? parseElectricityRef(String input) {
  var text = jsToUpperCase(toAsciiDigits(input).replaceAll(_separators, ''));
  String? urbanRural;
  final letter = _withLetter.firstMatch(text);
  if (letter != null) {
    text = letter.group(1)!;
    urbanRural = letter.group(2);
  }
  if (!_allDigits.hasMatch(text)) return null;

  if (text.length == 14) {
    final formatted =
        '${text.substring(0, 2)} ${text.substring(2, 7)} ${text.substring(7)}';
    return ElectricityRef(
      type: ElectricityRefType.reference,
      digits: text,
      formatted: urbanRural != null ? '$formatted $urbanRural' : formatted,
      urbanRural: urbanRural,
      disco: null,
    );
  }
  if (urbanRural != null) return null;
  if (text.length == 13 && text.startsWith('04000')) {
    return ElectricityRef(
      type: ElectricityRefType.kElectric,
      digits: text,
      formatted: text,
      urbanRural: null,
      disco: 'K-Electric',
    );
  }
  if (text.length == 10 || text.length == 11) {
    return ElectricityRef(
      type: ElectricityRefType.customerId,
      digits: text,
      formatted: text,
      urbanRural: null,
      disco: null,
    );
  }
  return null;
}