php preg_match特殊模式

时间:2013-04-13 06:10:39

标签: php preg-match

我想构建一个if语句,该语句读取url参数并检查它是否符合以下特殊模式:

Single capital letter (A to E), followed by
Single capital letter (R, S, H, or T), followed by
One digit (1 or 2), followed by
Two digits (01 to 18), followed by
Two digits (01 to 60), followed by
Single capital letter (X or Y), followed by
Pair of float numbers separated by a comma (5.8234,11.231134)

匹配模式的示例:

AH20340X22.14,43.8241212
CT11154Y1.431212,11.413
ES10113X11.512341,55.134513

1 个答案:

答案 0 :(得分:1)

您无法使用正则表达式(有限状态机)检查数字是否在“01和18之间”,因此您必须作弊。

所以你必须写:

Single capital letter (A to E) => [A-E]
Single capital letter (R, S, H, or T) => [RSHT]
One digit (1 or 2) => [12]
Two digits (01 to 18) => ([0][1-9]|[1][0-8])
Two digits (01 to 60) => ([0][1-9]|[1-5][0-9]|60)
Single capital letter (X or Y) => [XY]
Float number => [0-9]+(\.[0-9]+)?
Comma => ,
Float number => [0-9]+(\.[0-9]+)?

所以你最终会得到:

/[A-E][RSHT][12]([0][1-9]|[1][0-8])([0][1-9]|[1-5][0-9]|60)[XY][0-9]+(\.[0-9]+)?,[0-9]+(\.[0-9]+)?/