• Advertisement

Archived

This topic is now archived and is closed to further replies.

re trouble (edit: found problem)

This topic is 5100 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to match the timestamp in the parenthesis of the following line and regular expression (Python code):
line="Screenname (7:56:56 PM): hey"
time=re.match(r"\(\w+\)", line).group() #wtf
  
It isn't returning any matches. Anynone see whats wrong with my re? Edit: fixed it. I was using re.match instead of re.search. [edited by - thedustbustr on March 7, 2004 10:36:35 AM]

Share this post


Link to post
Share on other sites
Advertisement
In Perl, and I assume it works the same in Python, \w means [0-9a-zA-Z_], but neither '':'' not '' '' is a part of that set and the timestamp includes those characters. To match everything inside the parenthesis, use something like this instead.

\([^\)]+\)

Not tried it, but the ideas is to match any character not equal to a closing paremthesis.

Share this post


Link to post
Share on other sites
That seems like it should work, but it''s not producing any matches. Here''s my entire function, in case thats the problem:
line="Screenname (7:56:56 PM): hey"
#time=re.match(r"\(\w+\)", line)
#time=re.match(r"\(.+\)", line)
time=re.match(r"\([^\)]+\)", line)
if time!=None: time=time.group()
else: print "no match"
sn=string.split(line, "(", 1)[0]
msg=string.split(line, ":", 3)[3]
print "TIME:%s\tSN:%s\tMSG:%s"%(time, sn, msg),

Share this post


Link to post
Share on other sites

  • Advertisement