总结
此正则表达式假定您的源号码是 4 位字符串。它将查找第一个逗号分隔的数字在数值上大于第二个数字的实例。
如所写,此正则表达式假定您正在使用忽略空格或换行符的“x”标志。
正则表达式
^(?=\d{4},\d{4}(?:\D|\Z))(?:(?:
[9]\d*,[012345678]\d*|
[89]\d*,[01234567]\d*|
[789]\d*,[0123456]\d*|
[6789]\d*,[012345]\d*|
[56789]\d*,[01234]\d*|
[456789]\d*,[0123]\d*|
[3456789]\d*,[012]\d*|
[23456789]\d*,[01]\d*|
[123456789]\d*,[0]\d*
)|
(?<a>\d{1})(?:
[9]\d*,\k<a>[012345678]\d*|
[89]\d*,\k<a>[01234567]\d*|
[789]\d*,\k<a>[0123456]\d*|
[6789]\d*,\k<a>[012345]\d*|
[56789]\d*,\k<a>[01234]\d*|
[456789]\d*,\k<a>[0123]\d*|
[3456789]\d*,\k<a>[012]\d*|
[23456789]\d*,\k<a>[01]\d*|
[123456789]\d*,\k<a>[0]\d*
)|
(?<b>\d{2})(?:
[9]\d*,\k<b>[012345678]\d*|
[89]\d*,\k<b>[01234567]\d*|
[789]\d*,\k<b>[0123456]\d*|
[6789]\d*,\k<b>[012345]\d*|
[56789]\d*,\k<b>[01234]\d*|
[456789]\d*,\k<b>[0123]\d*|
[3456789]\d*,\k<b>[012]\d*|
[23456789]\d*,\k<b>[01]\d*|
[123456789]\d*,\k<b>[0]\d*
)|
(?<c>\d{3})(?:
[9]\d*,\k<c>[012345678]\d*|
[89]\d*,\k<c>[01234567]\d*|
[789]\d*,\k<c>[0123456]\d*|
[6789]\d*,\k<c>[012345]\d*|
[56789]\d*,\k<c>[01234]\d*|
[456789]\d*,\k<c>[0123]\d*|
[3456789]\d*,\k<c>[012]\d*|
[23456789]\d*,\k<c>[01]\d*|
[123456789]\d*,\k<c>[0]\d*
))
示例
http://www.rubular.com/r/XjBNBQIzGP
示例文本
Born,Died
1852,1891
1862,1862
1902,1785
1111,1111
1111,1110
2222,2202
3333,3033
4444,0444
123,456
1234,567
123,4567
456,123
4567,123
456,1234
4567,1234
样本捕获
[0][0] = 1902,1785
[0][a] = 1
[0][b] =
[0][c] =
[1][0] = 1111,1110
[1][a] =
[1][b] =
[1][c] = 111
[2][0] = 2222,2202
[2][a] =
[2][b] = 22
[2][c] =
[3][0] = 3333,3033
[3][a] = 3
[3][b] =
[3][c] =
[4][0] = 4444,0444
[4][a] =
[4][b] =
[4][c] =
[5][0] = 4567,1234
[5][a] =
[5][b] =
[5][c] =
简短说明
正则表达式的开头有一个前瞻来验证我们确实有两个 4 位数字。
四个代码块测试每个位置以验证一个数字是否大于另一个数字。第二个、第三个和第四个块包含一个命名的反向引用(分别为 a、b、c)。此反向引用确保前导数字相同。
更详细的解释
NODE EXPLANATION
----------------------------------------------------------------------
^ the beginning of a "line"
----------------------------------------------------------------------
(?= look ahead to see if there is:
----------------------------------------------------------------------
\d{4} digits (0-9) (4 times)
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
\d{4} digits (0-9) (4 times)
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
\D non-digits (all but 0-9)
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
\Z before an optional \n, and the end of
the string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
) end of look-ahead
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
[9] any character of: '9'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
[012345678] any character of: '0', '1', '2', '3',
'4', '5', '6', '7', '8'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[89] any character of: '8', '9'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
[01234567] any character of: '0', '1', '2', '3',
'4', '5', '6', '7'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[789] any character of: '7', '8', '9'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
[0123456] any character of: '0', '1', '2', '3',
'4', '5', '6'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[6789] any character of: '6', '7', '8', '9'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
[012345] any character of: '0', '1', '2', '3',
'4', '5'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[56789] any character of: '5', '6', '7', '8',
'9'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
[01234] any character of: '0', '1', '2', '3',
'4'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[456789] any character of: '4', '5', '6', '7',
'8', '9'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
[0123] any character of: '0', '1', '2', '3'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[3456789] any character of: '3', '4', '5', '6',
'7', '8', '9'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
[012] any character of: '0', '1', '2'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[23456789] any character of: '2', '3', '4', '5',
'6', '7', '8', '9'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
[01] any character of: '0', '1'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
[123456789] any character of: '1', '2', '3', '4',
'5', '6', '7', '8', '9'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
, ','
----------------------------------------------------------------------
[0] any character of: '0'
----------------------------------------------------------------------
\d* digits (0-9) (0 or more times
(matching the most amount possible))
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
) end of grouping