手机机身码(IMEI)

2024-06-23 21:54:01 687
正则表达式匹配手机机身码(IMEI)

正则表达式

/^\d{15,17}$/

解析

  • ^:匹配字符串的开始。
  • \d:匹配一个数字字符(0-9)。
  • {15,17}:量词,匹配前面的数字字符 15 到 17 次。
  • $:匹配字符串的结束。

示例

匹配成功的示例

  • "123456789012345"(15 个数字)
  • "1234567890123456"(16 个数字)
  • "12345678901234567"(17 个数字)

匹配失败的示例

  • "12345678901234"(不足 15 个数字)
  • "123456789012345678"(超过 17 个数字)
  • "12345678901234a"(包含非数字字符)
  • "12345678901234 "(包含空格)

用法

Python

import re

pattern = r'^\d{15,17}$'
test_strings = ["123456789012345", "1234567890123456", "12345678901234567", "12345678901234", "123456789012345678", "12345678901234a", "12345678901234 "]

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

JavaScript

const pattern = /^\d{15,17}$/;
const testStrings = ["123456789012345", "1234567890123456", "12345678901234567", "12345678901234", "123456789012345678", "12345678901234a", "12345678901234 "];

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 = "^\\d{15,17}$";
        String[] testStrings = {"123456789012345", "1234567890123456", "12345678901234567", "12345678901234", "123456789012345678", "12345678901234a", "12345678901234 "};
        
        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 = @"^\d{15,17}$";
        string[] testStrings = {"123456789012345", "1234567890123456", "12345678901234567", "12345678901234", "123456789012345678", "12345678901234a", "12345678901234 "};
        
        foreach (string testString in testStrings) {
            if (Regex.IsMatch(testString, pattern)) {
                Console.WriteLine($"'{testString}': 匹配成功");
            } else {
                Console.WriteLine($"'{testString}': 匹配失败");
            }
        }
    }
}

PHP

<?php
$pattern = '/^\d{15,17}$/';
$testStrings = ["123456789012345", "1234567890123456", "12345678901234567", "12345678901234", "123456789012345678", "12345678901234a", "12345678901234 "];

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

Ruby

pattern = /^\d{15,17}$/
test_strings = ["123456789012345", "1234567890123456", "12345678901234567", "12345678901234", "123456789012345678", "12345678901234a", "12345678901234 "]

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