24 小时 unix 时间戳的正则表达式
regex for 24h unix timestamp
我想为 24 小时 unix 时间戳创建一个正则表达式,起始于:01/01/2015 00:00:00 **(1420066800)** to 01/01/2015 23:59:59 **(1420153199)**
,相差 86399 秒。在 unix 时间戳格式中。
我正在使用 range_regex
python 库,但它对于如此大的范围来说是错误的。 range_to_pattern
方法 (range_to_pattern(1420066800, 1420153199)
) 将生成一个正则表达式:1420[0-1][5-6][3-6][1-8]\d{2}
这对于创建正则表达式的静态边界来说很好,但是当涉及到像这样的值时:1420159111
因为左边的第 7 位数字 (9) 不在第三个范围组 ([3-6]) 中。
有人可以提供更好的 python3 库或解决方法来创建 86400 秒的正则表达式。一天?
根据我上面的评论,您使用了该库中的错误函数。
您应该使用以下内容:
range_to_regex(1420066800, 1420153199)
这个 returns 正确的正则表达式:
142006680\d|14200668[1-9]\d|14200669\d{2}|142006[7-9]\d{3}|14200[7-9]\d{4}|14201[0-4]\d{4}|142015[0-2]\d{3}|1420153[0-1]\d{2}
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"1420([0]([6]([6]([8]([0][0-9])|[9][0-9]{2})|[7-9][0-9]{3})|[7-9][0-9]{4})|[1]([5]([3]([1]([9][0-9]|[0-8][0-9]{1})|[0][0-9]{2})|[0-2][0-9]{3})|[0-4][0-9]{4}))"
test_str = ("01/01/2015 00:00:00 (1420066800) до 01/01/2015 23:59:59 (1420153199)\n\n"
"1420016799 -no\n"
"1420066799 -no\n"
"1420066800 -yes\n"
"1420066801 -yes\n"
"1420067820 -yes\n"
"1420067920 -yes\n"
"1420073199 -yes\n"
"1420103199 -yes\n"
"1420152191 -yes\n"
"1420153181 -yes\n"
"1420153199 -yes\n"
"1420153200 -no\n"
"1420163199 -no")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
我想为 24 小时 unix 时间戳创建一个正则表达式,起始于:01/01/2015 00:00:00 **(1420066800)** to 01/01/2015 23:59:59 **(1420153199)**
,相差 86399 秒。在 unix 时间戳格式中。
我正在使用 range_regex
python 库,但它对于如此大的范围来说是错误的。 range_to_pattern
方法 (range_to_pattern(1420066800, 1420153199)
) 将生成一个正则表达式:1420[0-1][5-6][3-6][1-8]\d{2}
这对于创建正则表达式的静态边界来说很好,但是当涉及到像这样的值时:1420159111
因为左边的第 7 位数字 (9) 不在第三个范围组 ([3-6]) 中。
有人可以提供更好的 python3 库或解决方法来创建 86400 秒的正则表达式。一天?
根据我上面的评论,您使用了该库中的错误函数。
您应该使用以下内容:
range_to_regex(1420066800, 1420153199)
这个 returns 正确的正则表达式:
142006680\d|14200668[1-9]\d|14200669\d{2}|142006[7-9]\d{3}|14200[7-9]\d{4}|14201[0-4]\d{4}|142015[0-2]\d{3}|1420153[0-1]\d{2}
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"1420([0]([6]([6]([8]([0][0-9])|[9][0-9]{2})|[7-9][0-9]{3})|[7-9][0-9]{4})|[1]([5]([3]([1]([9][0-9]|[0-8][0-9]{1})|[0][0-9]{2})|[0-2][0-9]{3})|[0-4][0-9]{4}))"
test_str = ("01/01/2015 00:00:00 (1420066800) до 01/01/2015 23:59:59 (1420153199)\n\n"
"1420016799 -no\n"
"1420066799 -no\n"
"1420066800 -yes\n"
"1420066801 -yes\n"
"1420067820 -yes\n"
"1420067920 -yes\n"
"1420073199 -yes\n"
"1420103199 -yes\n"
"1420152191 -yes\n"
"1420153181 -yes\n"
"1420153199 -yes\n"
"1420153200 -no\n"
"1420163199 -no")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches):
matchNum = matchNum + 1
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.