[英]Python Regex use during specific string search in a text file
I have to find an expression in a text file like : StartTime="4/11/2013 8:11:20:965" and EndTime="4/11/2013 8:11:22:571"
我必须在文本文件中找到一个表达式,如:
StartTime="4/11/2013 8:11:20:965" and EndTime="4/11/2013 8:11:22:571"
So I used the regex expression 所以我使用了正则表达式
r'(\w)="(\d{1,2}/\d{1,2}/\d{4} \d{1,2}:\d{1,2}:\d{1,2}:\d{2,3})"'
Thanks again to eumiro for his help earlier ( Retrieve randomly preformatted text from Text File ) 再次感谢eumiro的帮助( 从文本文件中随机检索预先格式化的文本 )
But I can't find anything in my file, and I checked it was there. 但我在我的档案中找不到任何东西,我检查过它。
I can't go trhough 'GetDuration lvl 1' with it actually. 实际上,我无法用它来实现'GetDuration lvl 1'。
I tried to simplify my regex as r'(\\d)'
, and it worked to lvl 4, so I thought it could be and issue with eventually protected "
but I didn't see anything about this in python doc. 我试图将我的正则表达式简化为
r'(\\d)'
,并且它工作到lvl 4,所以我认为它可能并且最终受到保护的问题"
但我在python doc中没有看到任何关于此的内容。
What am I missing ? 我错过了什么?
Regular_Exp = r'(\w)="(\d{1,2}/\d{1,2}/\d{4} \d{1,2}:\d{1,2}:\d{1,2}:\d{2,3})"'
def getDuration(timeCode1, timeCode2)
duration =0
c = ''
print 'GetDuration lvl 0'
for c in str(timeCode1) :
m = re.search(Regular_Exp, c)
print 'GetDuration lvl 1'
if m:
print 'GetDuration lvl 2'
for text in str(timeCode2) :
print 'GetDuration lvl 3'
n = re.search(Regular_Exp, c)
if n:
print 'GetDuration lvl 4'
timeCode1Split = timeCode1.split(' ')
timeCode1Date = timeCode1Split[0].split('/')
timeCode1Heure = timeCode1Split[1].split(':')
timeCode2Split = timeCode2.split(' ')
timeCode2Date = timeCode2Split[0].split('/')
timeCode2Heure = timeCode2Split[1].split(':')
timeCode1Date = dt.datetime(timeCode1Date[0], timeCode1Date[1], timeCode1Date[2], timeCode1Heure[0], timeCode1Heure[0], timeCode1Heure[0], tzinfo=utc)
timeCode2Date = dt.datetime(timeCode2Date[0], timeCode2Date[1], timeCode2Date[2], timeCode2Heure[0], timeCode2Heure[0], timeCode2Heure[0], tzinfo=utc)
print 'TimeCode'
print timeCode1Date
print timeCode2Date
duration += timeCode1Date - timeCode2Date
return duration
for c in str(timeCode1) :
m = re.search(Regular_Exp, c)
...
for x in str(something)
means you're iterating something
character by character (one character=1 length str
at a time), and no regex can match with that. for x in str(something)
意味着你迭代something
逐个字符(一个字符= 1个长度str
在时间),并没有正则表达式可以与匹配。
Maybe this exp should help: 也许这个exp应该有帮助:
"(\w+?)=\"(.+?)\""
TO use: 使用:
>>> string = u'StartTime="4/11/2013 8:11:20:965" and EndTime="4/11/2013 8:11:22:571"'
>>> regex = re.compile("(\w+?)=\"(.+?)\"")
# Run findall
>>> regex.findall(string)
[(u'StartTime', u'4/11/2013 8:11:20:965'), (u'EndTime', u'4/11/2013 8:11:22:571')]
Also, for c in str(timeCode1)
, try printing c
, you are going one character at a time, not a good idea with regex.. 另外,
for c in str(timeCode1)
,尝试打印c
,你一次只能输入一个字符,这对于正则表达式来说不是一个好主意。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.