以下正则表达式完美无缺
[\s\S]*[0RECALL]?[\s\S]\d(?P<fpName>[R|U|J|L|I|N]\d\d\d\d),(?P<fpReceivedTimestamp>\d\d\/\d\d\/\d\d,\d\d\d\d)Z,[^,]*,(?P<basic_weight>\d\d\d\d\d\d)\/(?P<payload>\d\d\d\d\d[\d]?),
代表
0RECALL,J2516\n1J2516,04/20/16,1336Z,N103XA /CL30 ,123456/123456,
但我需要一个正则表达式 最后的数字是可选的123456/123456或------ / ------
所以文字可以是
0RECALL,J2516\n1J2516,04/20/16,1336Z,N103XA /CL30 ,123456/123456,
or
0RECALL,J2516\n1J2516,04/20/16,1336Z,N103XA /CL30 ,------/------,
我应该对我的正则表达式做些什么改变。目前我正在使用以下在线工具寻求帮助
答案 0 :(得分:4)
| (管道)用于OR。
我也用[\ d] {6}
替换了一些\ d \ d \ d \ d \ d \ d[\s\S]*[0RECALL]?[\s\S]\d(?P<fpName>[R|U|J|L|I|N]\d\d\d\d),(?P<fpReceivedTimestamp>\d\d\/\d\d\/\d\d,\d\d\d\d)Z,[^,]*,(?P<basic_weight>(\d\d\d\d\d\d)|[-]{6})\/(?P<payload>([\d]{5,6})|[-]{5,6}),
编辑:
答案 1 :(得分:1)
您可以将正则表达式的这一部分(?P<basic_weight>\d\d\d\d\d\d)\/(?P<payload>\d\d\d\d\d[\d]?),
更改为:
(?P<basic_weight>\d\d\d\d\d\d|?P<basic_weight>-{5})\/(?P<payload>\d\d\d\d\d[\d]|?P<basic_weight>-{5}),
在这里,您使用|
“pipe”运算符将五位数作为可选项。