我当前正在尝试打印通常在另一个字符串之前出现的字符串,例如"12/07/201911 : 00Type of Occurrence"
。在这里,我尝试打印时间"11 : 00"
,该时间总是在文本"Type of Occurrence"
之前。
我已经尝试过在标识符之前打印所有内容。我只想在标识符前打印7个字符。
import re
A="12/07/2019 11 : 00Type of Occurrence"
print(A.split('Type of Occurrence', 1)[0].replace('.', '').upper())
它打印:
12/07/2019 11 : 00
答案 0 :(得分:1)
尝试一下:
A="12/07/2019 11 : 00Type of Occurrence"
print(" ".join(A.split("Type of Occurrence")[0].split()[1:]))
输出:
11 : 00
过程:
A.split("Type of Occurrence")[0]
给出"12/07/2019 11 : 00"
"12/07/2019 11 : 00".split()[1:]
给出['11', ':', '00']
" ".join(['11', ':', '00'])
给出11 : 00
答案 1 :(得分:1)
尝试一下:
var l1 = new Date("2019-07-30");
var l2 = new Date("2019-07-13");
function filter(date) {
if (date instanceof Date && l1 instanceof Date && l2 instanceof Date) {
return l1.getTime() < l2.getTime() ? !(date.getTime() >= l1.getTime() && date.getTime() <= l2.getTime()) : !(date.getTime() >= l2.getTime() && date.getTime() <= l1.getTime())
} else {
return false;
}
}
console.log(filter(new Date("2019-07-13")));
console.log(filter(new Date("2019-07-12")));
答案 2 :(得分:1)
如果冒号之间的空格可能不同,则可以在时间部分使用捕获组并匹配“发生类型”:
((?:0[0-9]|1[0-9]|2[0-3])\s*:\s*[0-5][0-9])Type of Occurrence
答案 3 :(得分:0)
基本思路:
A="12/07/2019 11 : 00Type of Occurrence"
A = A.split('Type of Occurrence', 1)[0].replace('.', '').upper()
A[-7:]
>>> '11 : 00'
这是您想要的吗?
答案 4 :(得分:0)
我只想在标识符前打印7个字符。
您可以使用此正则表达式:
re.findall
{{1}}打印捕获的组(如果正则表达式中可用)。