邮政编码(中国)

2024-06-23 21:38:50 416
匹配邮政编码(中国)的正则表达式

正则表达式

/^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$/

解析

  • ^:匹配字符串的开始。
  • ( ... ):捕获组,用于将括号内的内容视为一个单元。
  • |:逻辑或,表示前后两部分中的任意一个都可以匹配。
  • 各个匹配条件
    • 0[1-7]:匹配两位数,第一位是0,第二位是1到7。
    • 1[0-356]:匹配两位数,第一位是1,第二位是0、3、5或6。
    • 2[0-7]:匹配两位数,第一位是2,第二位是0到7。
    • 3[0-6]:匹配两位数,第一位是3,第二位是0到6。
    • 4[0-7]:匹配两位数,第一位是4,第二位是0到7。
    • 5[1-7]:匹配两位数,第一位是5,第二位是1到7。
    • 6[1-7]:匹配两位数,第一位是6,第二位是1到7。
    • 7[0-5]:匹配两位数,第一位是7,第二位是0到5。
    • 8[013-6]:匹配两位数,第一位是8,第二位是0、1、3、4、5或6。
  • \d{4}:匹配四个数字(0-9)。
  • $:匹配字符串的结束。

用法

Python

import re

pattern = r'^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$'
test_strings = ["011234", "157890", "220000", "360987", "475678", "517899", "618456", "705123", "804567", "091234", "185678", "290000", "376789", "488888", "559876", "678901", "799123", "827890", "12345", "1234567"]

for test_string in test_strings:
    if re.match(pattern, test_string):
        print(f"'{test_string}': 匹配成功")
    else:
        print(f"'{test_string}': 匹配失败")

JavaScript

const pattern = /^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$/;
const testStrings = ["011234", "157890", "220000", "360987", "475678", "517899", "618456", "705123", "804567", "091234", "185678", "290000", "376789", "488888", "559876", "678901", "799123", "827890", "12345", "1234567"];

testStrings.forEach(testString => {
    if (pattern.test(testString)) {
        console.log(`'${testString}': 匹配成功`);
    } else {
        console.log(`'${testString}': 匹配失败`);
    }
});

Java

import java.util.regex.*;

public class RegexTest {
    public static void main(String[] args) {
        String pattern = "^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\\d{4}$";
        String[] testStrings = {"011234", "157890", "220000", "360987", "475678", "517899", "618456", "705123", "804567", "091234", "185678", "290000", "376789", "488888", "559876", "678901", "799123", "827890", "12345", "1234567"};
        
        for (String testString : testStrings) {
            if (Pattern.matches(pattern, testString)) {
                System.out.println("'" + testString + "': 匹配成功");
            } else {
                System.out.println("'" + testString + "': 匹配失败");
            }
        }
    }
}

C#

using System;
using System.Text.RegularExpressions;

class Program {
    static void Main() {
        string pattern = @"^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$";
        string[] testStrings = {"011234", "157890", "220000", "360987", "475678", "517899", "618456", "705123", "804567", "091234", "185678", "290000", "376789", "488888", "559876", "678901", "799123", "827890", "12345", "1234567"};
        
        foreach (string testString in testStrings) {
            if (Regex.IsMatch(testString, pattern)) {
                Console.WriteLine($"'{testString}': 匹配成功");
            } else {
                Console.WriteLine($"'{testString}': 匹配失败");
            }
        }
    }
}

PHP

<?php
$pattern = '/^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$/';
$testStrings = ["011234", "157890", "220000", "360987", "475678", "517899", "618456", "705123", "804567", "091234", "185678", "290000", "376789", "488888", "559876", "678901", "799123", "827890", "12345", "1234567"];

foreach ($testStrings as $testString) {
    if (preg_match($pattern, $testString)) {
        echo "'$testString': 匹配成功\n";
    } else {
        echo "'$testString': 匹配失败\n";
    }
}
?>

Ruby

pattern = /^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$/
test_strings = ["011234", "157890", "220000", "360987", "475678", "517899", "618456

", "705123", "804567", "091234", "185678", "290000", "376789", "488888", "559876", "678901", "799123", "827890", "12345", "1234567"]

test_strings.each do |test_string|
  if pattern.match?(test_string)
    puts "'#{test_string}': 匹配成功"
  else
    puts "'#{test_string}': 匹配失败"
  end
end