您的号码可以通过以下正则表达式匹配:
^ *(1[56])? *(\d{5}) *(0?[56]) *([A-Z]) *(\d{1,2}) *$
这是一个粗略的细分。我命名了识别号的部分。您可能有更合适的名称。:
^ * #Start the match at the beginning of a string and consume all leading spaces if any.
(1[56])? #GROUP 1: The Id number prefix. (Optional)
* #Consume spaces if any.
(\d{5}) #GROUP 2: The five digit identifier code.
* #Consume spaces if any.
(0?[56]) #GROUP 3: The two digit indicator code.
* #Consume spaces if any.
([A-Z]) #GROUP 4: The letter code.
* #Consume spaces if any.
(\d{1,2}) #GROUP 5: The end code.
*$ #End the match with remaining spaces and the end of the string.
你没有提到你使用的语言。这是我在 C# 中编写的一个函数,它使用这个正则表达式重新格式化输入标识号。
private string FormatIdentificationNumber(string inputIdNumber) {
const string DEFAULT_PREFIX = "16";
const string REGEX_ID_NUMBER = @"^ *(1[56])? *(\d{5}) *(0?[56]) *([A-Z]) *(\d{1,2}) *$";
const int REGEX_GRP_PREFIX = 1;
const int REGEX_GRP_IDENTIFIER = 2;
const int REGEX_GRP_INDICATOR = 3;
const int REGEX_GRP_LETTER_CODE = 4;
const int REGEX_GRP_END_CODE = 5;
Match m = Regex.Match(inputIdNumber, REGEX_ID_NUMBER, RegexOptions.IgnoreCase);
if (!m.Success) return inputIdNumber;
string prefix = m.Groups[REGEX_GRP_PREFIX].Value.Length == 0 ? DEFAULT_PREFIX : m.Groups[REGEX_GRP_PREFIX].Value;
string identifier = m.Groups[REGEX_GRP_IDENTIFIER].Value;
string indicator = m.Groups[REGEX_GRP_INDICATOR].Value.PadLeft(2, '0');
string letterCode = m.Groups[REGEX_GRP_LETTER_CODE].Value.ToUpper();
string endCode = m.Groups[REGEX_GRP_END_CODE].Value.PadLeft(2, '0');
return String.Concat(prefix, identifier, indicator, letterCode, endCode);
}