cpp/src/phonenumbers/phonenumberutil.cc
// Helper initialiser method to create the regular-expression pattern to match
// extensions. Note that:
// - There are currently six capturing groups for the extension itself. If this
// number is changed, MaybeStripExtension needs to be updated.
// - The only capturing groups should be around the digits that you want to
// capture as part of the extension, or else parsing will fail!
Helper initialiser method to create the regular-expression pattern to match
extensions. Note that:
There are currently six capturing groups for the extension itself. If this
number is changed, MaybeStripExtension needs to be updated.
The only capturing groups should be around the digits that you want to
capture as part of the extension, or else parsing will fail!
Helper initialiser method to create the regular - expression pattern to match extensions . Note that : There are currently six capturing groups for the extension itself . If this number is changed MaybeStripExtension needs to be updated . The only capturing groups should be around the digits that you want to capture as part of the extension or else parsing will fail!
std::string CreateExtnPattern(bool for_parsing) {
  int ext_limit_after_explicit_label = 20;
  int ext_limit_after_likely_label = 15;
  int ext_limit_after_ambiguous_char = 9;
  int ext_limit_when_not_sure = 6;
  string explicit_ext_labels =
      "(?:e?xt(?:ensi(?:o\xCC\x81?|\xC3\xB3))?n?|(?:\xEF\xBD\x85)?"
      "\xEF\xBD\x98\xEF\xBD\x94(?:\xEF\xBD\x8E)?|\xD0\xB4\xD0\xBE\xD0\xB1|"
      "anexo)";
  string ambiguous_ext_labels =
      "(?:[x\xEF\xBD\x98#\xEF\xBC\x83~\xEF\xBD\x9E]|int|"
      "\xEF\xBD\x89\xEF\xBD\x8E\xEF\xBD\x94)";
  string ambiguous_separator = "[- ]+";
  string rfc_extn = StrCat(kRfc3966ExtnPrefix,
                           ExtnDigits(ext_limit_after_explicit_label));
  string explicit_extn = StrCat(
      kPossibleSeparatorsBetweenNumberAndExtLabel,
      explicit_ext_labels, kPossibleCharsAfterExtLabel,
      ExtnDigits(ext_limit_after_explicit_label),
      kOptionalExtSuffix);
  string ambiguous_extn = StrCat(
      kPossibleSeparatorsBetweenNumberAndExtLabel,
      ambiguous_ext_labels, kPossibleCharsAfterExtLabel,
      ExtnDigits(ext_limit_after_ambiguous_char),
      kOptionalExtSuffix);
  string american_style_extn_with_suffix = StrCat(
      ambiguous_separator, ExtnDigits(ext_limit_when_not_sure), "#");
  string extension_pattern = StrCat(
      rfc_extn, "|",
      explicit_extn, "|",
      ambiguous_extn, "|",
      american_style_extn_with_suffix);
  if (for_parsing) {
    string auto_dialling_and_ext_labels_found = "(?:,{2}|;)";
    string possible_separators_number_extLabel_no_comma = "[ \xC2\xA0\\t]*";
    string auto_dialling_extn = StrCat(
      possible_separators_number_extLabel_no_comma,
      auto_dialling_and_ext_labels_found, kPossibleCharsAfterExtLabel,
      ExtnDigits(ext_limit_after_likely_label),
      kOptionalExtSuffix);
    string only_commas_extn = StrCat(
      possible_separators_number_extLabel_no_comma,
      "(?:,)+", kPossibleCharsAfterExtLabel,
      ExtnDigits(ext_limit_after_ambiguous_char),
      kOptionalExtSuffix);
    return StrCat(extension_pattern, "|",
                  auto_dialling_extn, "|",
                  only_commas_extn);
  }
  return extension_pattern;
}
std :: string CreateExtnPattern ( bool for_parsing ) { int ext_limit_after_explicit_label = 20 ; int ext_limit_after_likely_label = 15 ; int ext_limit_after_ambiguous_char = 9 ; int ext_limit_when_not_sure = 6 ; string explicit_ext_labels = " \xCC \x81 \xC3 \xB3 \xEF \xBD \x85 " " \xEF \xBD \x98 \xEF \xBD \x94 \xEF \xBD \x8E \xD0 \xB4 \xD0 \xBE \xD0 \xB1 " " " ; string ambiguous_ext_labels = " \xEF \xBD \x98 \xEF \xBC \x83 \xEF \xBD \x9E " " \xEF \xBD \x89 \xEF \xBD \x8E \xEF \xBD \x94 " ; string ambiguous_separator = " " ; string rfc_extn = StrCat ( kRfc3966ExtnPrefix , ExtnDigits ( ext_limit_after_explicit_label ) ) ; string explicit_extn = StrCat ( kPossibleSeparatorsBetweenNumberAndExtLabel , explicit_ext_labels , kPossibleCharsAfterExtLabel , ExtnDigits ( ext_limit_after_explicit_label ) , kOptionalExtSuffix ) ; string ambiguous_extn = StrCat ( kPossibleSeparatorsBetweenNumberAndExtLabel , ambiguous_ext_labels , kPossibleCharsAfterExtLabel , ExtnDigits ( ext_limit_after_ambiguous_char ) , kOptionalExtSuffix ) ; string american_style_extn_with_suffix = StrCat ( ambiguous_separator , ExtnDigits ( ext_limit_when_not_sure ) , " " ) ; string extension_pattern = StrCat ( rfc_extn , " " , explicit_extn , " " , ambiguous_extn , " " , american_style_extn_with_suffix ) ; if ( for_parsing ) { string auto_dialling_and_ext_labels_found = " " ; string possible_separators_number_extLabel_no_comma = " \xC2 \xA0 \\ " ; string auto_dialling_extn = StrCat ( possible_separators_number_extLabel_no_comma , auto_dialling_and_ext_labels_found , kPossibleCharsAfterExtLabel , ExtnDigits ( ext_limit_after_likely_label ) , kOptionalExtSuffix ) ; string only_commas_extn = StrCat ( possible_separators_number_extLabel_no_comma , " " , kPossibleCharsAfterExtLabel , ExtnDigits ( ext_limit_after_ambiguous_char ) , kOptionalExtSuffix ) ; return StrCat ( extension_pattern , " " , auto_dialling_extn , " " , only_commas_extn ) ; } return extension_pattern ; }
Helper initialiser method to create the regular-expression pattern to match
extensions.
Helper initialiser method to create the regular - expression pattern to match extensions .
// We cap the maximum length of an extension based on the ambiguity of the // way the extension is prefixed. As per ITU, the officially allowed // length for extensions is actually 40, but we don't support this since we // haven't seen real examples and this introduces many false interpretations // as the extension labels are not standardized. // Canonical-equivalence doesn't seem to be an option with RE2, so we allow // two options for representing any non-ASCII character like ó - the character // itself, and one in the unicode decomposed form with the combining acute // accent. // Here the extension is called out in a more explicit way, i.e mentioning it // obvious patterns like "ext.". // One-character symbols that can be used to indicate an extension, and less // commonly used or more ambiguous extension labels. // When extension is not separated clearly. // The first regular expression covers RFC 3966 format, where the extension is // added using ";ext=". The second more generic where extension is mentioned // with explicit labels like "ext:". In both the above cases we allow more // numbers in extension than any other extension labels. The third one // captures when single character extension labels or less commonly used // labels are present. In such cases we capture fewer extension digits in // order to reduce the chance of falsely interpreting two numbers beside each // other as a number + extension. The fourth one covers the special case of // American numbers where the extension is written with a hash at the end, // such as "- 503#". // Additional pattern that is supported when parsing extensions, not when // matching. // ",," is commonly used for auto dialling the extension when connected. // Semi-colon works in Iphone and also in Android to pop up a button with // the extension number following. // This is same as kPossibleSeparatorsBetweenNumberAndExtLabel, but not // matching comma as extension label may have it. // Here the first pattern is exclusive for extension autodialling formats // which are used when dialling and in this case we accept longer // extensions. However, the second pattern is more liberal on number of // commas that acts as extension labels, so we have strict cap on number of // digits in such extensions.