Jump to content
UBot Underground

Probelm trying to get time from text


Recommended Posts

Hi everyone,

 

Im not very good at Regex, and need a help solving my problem.

 

I had scraped this value in one var:

 

Sabado, 25 de Agosto de 2012 - 5:11:03 PM

 

I want to use a REGEX to extract the time from that variable. I want to get:

 

5:11:03

 

How I can get it?

 

PS. That value is hours:minutes:seconds.

 

Thanks

Link to post
Share on other sites

This should work.

 

(?<=- ).*?(?= PM)

 

Si no te funciona me avisas :).

 

 

Good solution, willywonka, but that will only grab the time if it is PM, to get the time for AM as well as PM use:

 

(?<=- ).*?(?= \wM)

 

Si no te funciona, avisa willywonka (lol)

Link to post
Share on other sites

Thanks for everyone helps!

 

Gracias willywonka!

 

Another question, from this:

 

5:09:03

 

How I can get Minutes in one var, and seconds in another var?

 

 

PS. I should learn Regex!

Link to post
Share on other sites

Thanks for everyone helps!

 

Gracias willywonka!

 

Another question, from this:

 

5:09:03

 

How I can get Minutes in one var, and seconds in another var?

 

 

PS. I should learn Regex!

 

 

 

list from text : as delimiter

list position 1

list position 2

no regex needed

 

Ptfg4

Link to post
Share on other sites

I did it in this way:

 

set(#time, $find regular expression(#clock, "(?<=- ).*?(?= PM| AM)"), "Global")

set(#hour, $substring(#time, 0, 1), "Global")

set(#min, $substring(#time, 2, 2), "Global")

set(#seg, $substring(#time, 5, 2), "Global")

 

Thanks all for your help!

Link to post
Share on other sites
  • 2 weeks later...
  • 4 weeks later...

[...]

set(#hour, $substring(#time, 0, 1), "Global")

set(#min, $substring(#time, 2, 2), "Global")

set(#seg, $substring(#time, 5, 2), "Global")[...]

Your code is 'assuming' that your scraped string has a fixed length, while in fact, due to the 2-digit hours, such as 10, 11 or 12... in fact the string's length varies!

You can do it almost as you coded, but you'll need to provide extra code to determine the length and change the substrings' starting/ending points, accordingly.

 

However, the $list from text using : as the delimiter looks much simpler to implement.

 

Another way might be to subtract the substrings using a $find index and looking for the ":" character as the delimiter.. etc...

Link to post
Share on other sites

Ahh...

 

Another way would be to pad an extra character (blank) at the beginning of the string, inside an IF (looking for its length to be equal to 7 chars) and then apply the exact same code, no matter what the hour is, except for the results you would add an extra $trim to the #hour resulting variable

 

Hope this helps

Link to post
Share on other sites

   set(#hour, $substring(#time, 0, $find index(#time, ":")), "Global")
   set(#time, $replace(#time, "{#hour}:", $nothing), "Global")
   set(#min, $substring(#time, 0, $find index(#time, ":")), "Global")
   set(#seg, $replace(#time, "{#min}:", $nothing), "Global")

^^^ The code above will work just the same, no matter if your scraped string is

   set(#time, "7:02:25", "Global")

or

   set(#time, "11:02:25", "Global")

 

However, while due to the source already giving you the hours part correctly, but the min/sec with leading zeros, you may want to replace those in the results, too, something like this:

 

   set(#min, $replace(#min, 0, $nothing), "Global")

 

So, finally, the code could look like this:

 

   set(#hour, $substring(#time, 0, $find index(#time, ":")), "Global")
   set(#time, $replace(#time, "{#hour}:", $nothing), "Global")
   set(#min, $substring(#time, 0, $find index(#time, ":")), "Global")
   set(#min, $replace(#min, 0, $nothing), "Global")
   set(#seg, $replace(#time, "{#min}:", $nothing), "Global")
   set(#seg, $replace(#seg, 0, $nothing), "Global")

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...