0

我想构建一个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
4

1 回答 1

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]+)?/
于 2013-04-13T06:22:10.370 回答