1

嗨,伙计,我是正则表达式的新手,我正在尝试为字符串值的模式创建表达式,规则如下

1) String must start with the 'O' or 'T'
2) After that there must be 9 digits 
3) After that there must be 'T'
4) After that alphanumeric string with specs and numbers and character'D' these can repeat any number of time in string of length 25 
5) Then character 'O'
6) Then string with numbers and spaces of length 5  

但是,我已经完成了所有条件,但对于条件4 ,我不知道该怎么做,因为它可以在长度为 25 的给定字符串中重复“D”任意次数。现在写,我已经调整了可选的“D”在它出现在字符串中的每个地方,这是一种非常冗长的正则表达式,所以我希望有人能在条件4方面帮助我。任何帮助都会很棒。

要匹配的字符串 -->

T011600062TO51D45D0399D0O 1807

最新的正则表达式 -->

(?x)((?:[OT]\d{9})(?:T\s*\d*\s*[0-9DO ]\d*\s*[OD0-9 ]\s*\d*[D0-9]\s*\d*[0-9OD ]\d*)(?:[O]\s*\d*)\b)

我对正则表达式的这一部分感到困惑

(?:T\s*\d*\s*[0-9DO ]\d*\s*[OD0-9 ]\s*\d*[D0-9]\s*\d*[0-9OD ]\d*)

这是正确的方法吗?

4

3 回答 3

0

条件 4 不清楚。

(O|T)\d{9}T[D\d]{25}O[\d ]{5}

于 2013-08-16T06:43:23.457 回答
0

也许是 D{0,25}。表示 D 重复 0~25 次。

于 2013-08-16T06:54:48.570 回答
0

您可以尝试所有匹配的模式,

(O|T)\d{9}T[DO \d]{0,25}O[\d ]{5}
于 2013-08-16T07:01:33.060 回答