返回

正则表达式教程:匹配火车车次

前端

一、正则解释

正则表达式:

/^[GCDZTSPKXLY1-9]d{1,4}$/

含义:

  • ^: 匹配字符串开头
  • [GCDZTSPKXLY1-9]: 匹配火车车次前缀,可以是 GDZTSPKXLY 或数字 1-9
  • d{1,4}: 匹配 1 到 4 位数字
  • $: 匹配字符串结尾

示例:

该正则表达式可以匹配以下火车车次:

  • G1868
  • D102
  • D9
  • Z5
  • Z24
  • Z17

二、使用场景

该正则表达式主要用于匹配火车车次,例如:

  • 验证用户输入的车次是否合法
  • 从文本中提取火车车次信息
  • 对火车车次进行分类和整理

三、代码示例

JavaScript

const regex = /^[GCDZTSPKXLY1-9]d{1,4}$/;

console.log(regex.test("G1868")); // true
console.log(regex.test("D102")); // true
console.log(regex.test("D9")); // true
console.log(regex.test("Z5")); // true
console.log(regex.test("Z24")); // true
console.log(regex.test("Z17")); // true
console.log(regex.test("X10")); // true

Java

import java.util.regex.Pattern;

public class TrainNumberValidation {

    public static boolean isValidTrainNumber(String trainNumber) {
        Pattern pattern = Pattern.compile("^[GCDZTSPKXLY1-9]d{1,4}
import java.util.regex.Pattern;

public class TrainNumberValidation {

    public static boolean isValidTrainNumber(String trainNumber) {
        Pattern pattern = Pattern.compile("^[GCDZTSPKXLY1-9]d{1,4}$");
        return pattern.matcher(trainNumber).matches();
    }

    public static void main(String[] args) {
        System.out.println(isValidTrainNumber("G1868")); // true
        System.out.println(isValidTrainNumber("D102")); // true
        System.out.println(isValidTrainNumber("D9")); // true
        System.out.println(isValidTrainNumber("Z5")); // true
        System.out.println(isValidTrainNumber("Z24")); // true
        System.out.println(isValidTrainNumber("Z17")); // true
        System.out.println(isValidTrainNumber("X10")); // true
    }
}
quot;); return pattern.matcher(trainNumber).matches(); } public static void main(String[] args) { System.out.println(isValidTrainNumber("G1868")); // true System.out.println(isValidTrainNumber("D102")); // true System.out.println(isValidTrainNumber("D9")); // true System.out.println(isValidTrainNumber("Z5")); // true System.out.println(isValidTrainNumber("Z24")); // true System.out.println(isValidTrainNumber("Z17")); // true System.out.println(isValidTrainNumber("X10")); // true } }

Python

import re

def is_valid_train_number(train_number):
    pattern = r"^[GCDZTSPKXLY1-9]d{1,4}
import re

def is_valid_train_number(train_number):
    pattern = r"^[GCDZTSPKXLY1-9]d{1,4}$"
    return re.match(pattern, train_number)

print(is_valid_train_number("G1868"))  # True
print(is_valid_train_number("D102"))  # True
print(is_valid_train_number("D9"))  # True
print(is_valid_train_number("Z5"))  # True
print(is_valid_train_number("Z24"))  # True
print(is_valid_train_number("Z17"))  # True
print(is_valid_train_number("X10"))  # True
quot;
return re.match(pattern, train_number) print(is_valid_train_number("G1868")) # True print(is_valid_train_number("D102")) # True print(is_valid_train_number("D9")) # True print(is_valid_train_number("Z5")) # True print(is_valid_train_number("Z24")) # True print(is_valid_train_number("Z17")) # True print(is_valid_train_number("X10")) # True

PHP

<?php

$regex = "/^[GCDZTSPKXLY1-9]d{1,4}$/";

echo preg_match($regex, "G1868") . "\n"; // 1
echo preg_match($regex, "D102") . "\n"; // 1
echo preg_match($regex, "D9") . "\n"; // 1
echo preg_match($regex, "Z5") . "\n"; // 1
echo preg_match($regex, "Z24") . "\n"; // 1
echo preg_match($regex, "Z17") . "\n"; // 1
echo preg_match($regex, "X10") . "\n"; // 1

?>