Shacky 2 Posted August 25, 2012 Report Share Posted August 25, 2012 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 Quote Link to post Share on other sites
jomark3 25 Posted August 26, 2012 Report Share Posted August 26, 2012 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) Quote Link to post Share on other sites
Shacky 2 Posted August 26, 2012 Author Report Share Posted August 26, 2012 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! Quote Link to post Share on other sites
pftg4 102 Posted August 26, 2012 Report Share Posted August 26, 2012 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 delimiterlist position 1list position 2no regex needed Ptfg4 Quote Link to post Share on other sites
Shacky 2 Posted August 26, 2012 Author Report Share Posted August 26, 2012 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! Quote Link to post Share on other sites
brusacco 20 Posted September 5, 2012 Report Share Posted September 5, 2012 Thanks ... I'm not REGEX expert, and this solves MANY problems, not only date or time ones.Thanks. Quote Link to post Share on other sites
Aymen 385 Posted September 29, 2012 Report Share Posted September 29, 2012 it is simple , no need for all that fuss , a simple regex code can do the thing for you[0-9]\:[0-9]{2}\:[0-9]{2} any more regex help , let me know Quote Link to post Share on other sites
VaultBoss 310 Posted September 29, 2012 Report Share Posted September 29, 2012 [...] 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... Quote Link to post Share on other sites
VaultBoss 310 Posted September 29, 2012 Report Share Posted September 29, 2012 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 Quote Link to post Share on other sites
VaultBoss 310 Posted September 29, 2012 Report Share Posted September 29, 2012 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") Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.