我正在用Notepad++编写一个时间表python脚本,以优化我的工作流程。我整理的数据如下:
2014-07-16_B_019 John Smith -Left(7:00pm)
2014-07-16_B_089 Jack Smith -Late(8:00pm)我希望为时间戳(例如,7:00 8pm和8 8pm)提供一个新字符串,.I确实使用了以下方法:
endNum = editor.getLineEndPosition(editor.lineFromPosition(editor.getCurrentPos())+1) #get position of line final character
stringMe = editor.getTextRange(endNum-7,endNum-1) #Convention must be met!但是硬编码选择范围是糟糕的做法,如果我说-Left(上午11点),就会中断。
发布于 2014-07-15 17:38:08
Python支持一些正则表达式;您可以使用editor.research查找特定模式的匹配位置。要找到你要找的东西,你可以试试。
r"\((\d{1,2}(?:[:.]\d{1,2})?(?:am|pm)?)\)$"在Regex101的演示和解释。
发布于 2014-07-15 17:37:56
正则表达式可能是提取此信息的最简单方法。
import re
title = "2014-07-16_B_019 John Smith -Left(7:00pm)"
matches = re.search(r"""(\d{4}- #the year
\d{2}- #the month
\d{2}) #the day
.* #anything
(\d{1,2}:\d{2}) #Hour:Minute
(am|pm) #am or pm""",title,re.X)
print matches.groups()https://stackoverflow.com/questions/24764286
复制相似问题