Leonick Posted July 8, 2009 Share Posted July 8, 2009 Hello everyone. I am scripting with autoit for 3 weeks now....and its a lot of fun. But atm i am stuck on regexp for several days now.....lol But i am closing in: I wanna extract the duration of the string: "Timer1: duration: 12d 08:34:19" With the help of PsaltyDS & ResNullius i was able to understand the minimal basics. My script is working so far: It extracts the days/hours/mins/sec perfectly, even when the time is only "12d 08:34" or "1d 08"....what was first the problem of it...but i understood to use the "?". Now the problem is, I cant make it work when there is no DAY e.g. 08:34:19 #include <Array.au3> $data = "Timer1: duration: 12d 08:34:22" $time = StringRegExp($data, "(?i)(?:.*?)(?:timer1:)(?:.*?)(\d{1,2})(?:d)(?:\D)(\d{2})(?::)?(\d{2})?(?::)?(\d{2})?", 1) _ArrayDisplay($time) Normally it should work like I did before, with the questionmark behind it: (\d{1,2})?(?)? But its not...lol If you could help me out, maybe I can get to this a bit closer. And one favour please! Dont make it more complicated for me....just help me correct my regexp-line,...and dont use a other way to reach it...causee my solution is very simple but easy to understand. Big Big thx. The next is only, if you still wanna do this...since its not that easy. ...to show me how i get the array into variables like: $day $hours $min $sec ....that would be awesome. cause working with the array is difficult, since i get errors, when the array is empty. e.g. when there are no days, i think it would be good, that the $time[0] would be empty,...but $time[1] would be full i dont really know how to NOT put an empty array into an variable without getting error. something to do with @error right, since IsArray only checks the whole array. But anyway, i hope i get it working after 5 days...lool regards Leonick Link to comment Share on other sites More sharing options...
Richard Robertson Posted July 9, 2009 Share Posted July 9, 2009 (edited) Try "(?i)timer1: duration:\s*(?:(\d{1,2})d\s*)?(?:(\d{2}):)?(?:(\d{2}):)?(\d{2})?" It returns the groups for the day without the "d", the hour, the minute, and the second. Edited July 9, 2009 by Richard Robertson Link to comment Share on other sites More sharing options...
James Posted July 9, 2009 Share Posted July 9, 2009 Regex is not easy to learn, however it does help to understand the basics. Good luck! Blog - Seriously epic web hosting - Twitter - GitHub - Cachet HQ Link to comment Share on other sites More sharing options...
Leonick Posted July 9, 2009 Author Share Posted July 9, 2009 (edited) Big thx Richard....but your script is not working at all! Do you test the stuff after posting? You forgot /s* after duration: ....then it works half way. -----RIGHT CODE NEXT POST PLS!!------- #include <Array.au3> $data = "Timer1: duration: 12d 08:34:09" $time = StringRegExp($data, "(?i)timer1:\s*duration:\s*(? :( \d{1,2})d\s*)?(? :( \d{2}) :) ?(? :( \d{2}) :) ?(\d{2})?",1) _ArrayDisplay($time) Now when there is no "DAY" then the time still get captured fine "08:34:09" array[0]="" array[1]="08" array[2]="34" array[3]="09" but when the time is only 21d 34:09 then result is not helpful, cause the array looks like this. array[0]="21" array[1]="34" array[2]= "" array[3]="09" now there's an empty dimension in it! and the minutes are now there, where the hours where before. how can i make a difference whats day/hour/min/and sec?? maybe i could try: array[0]=days e.g. "21" or "" array[1-3]=time e.g. "083419" or "3419" and then seperate everything with IF,..string or whatever. But i have no clue, how to get array[0] into a variable, even when its empty(So that there will be no error) now there is an EMPTY dimension in it!! Edited July 9, 2009 by Leonick Link to comment Share on other sites More sharing options...
Leonick Posted July 9, 2009 Author Share Posted July 9, 2009 Damn emoticons....always destroy code. Cant switch them off in edit. Code is so far: #include <Array.au3> $data = "Timer1: duration: 12d 08:34:09" $time = StringRegExp($data, "(?i)timer1:\s*duration:\s*(?:(\d{1,2})d\s*)?(?:(\d{2}):)?(?:(\d{2}):)?(\d{2})?",1) _ArrayDisplay($time) Link to comment Share on other sites More sharing options...
Leonick Posted July 9, 2009 Author Share Posted July 9, 2009 Problem solved!!! Thx to richard....and all the others,.....finally after xdays I solved the problem...and understood the basics of regexp...looool Now its working in EVERY CASE! #include <Array.au3> global $hours,$min,$sec $data = "Timer1: duration: 21d 02:18:09" $time = StringRegExp($data, "(?i)timer1:\s*duration:\s*(?:(\d{1,2})d\s*)?(?:(\d{2}):)?(?:(\d{2}):)?(\d{2})?", 1) $days = $time[0] $time = $time[1] & $time[2] & $time[3] If StringLen($time) = 6 Then $Hours = StringLeft($time, 2) $min = StringMid($time, 3, 2) $sec = StringRight($time, 2) ElseIf StringLen($time) = 4 Then $min = StringLeft($time, 2) $sec = StringRight($time, 2) ElseIf StringLen($time) = 2 Then $sec = $time EndIf ConsoleWrite("Days:" & $days & @LF) ConsoleWrite("Hours:" & $Hours & @LF) ConsoleWrite("Min:" & $min & @LF) ConsoleWrite("Sec:" & $sec & @LF) Link to comment Share on other sites More sharing options...
Richard Robertson Posted July 9, 2009 Share Posted July 9, 2009 (edited) My regex did have a \s* after duration. But also, no, I don't regularly test the code I write on here. Edited July 9, 2009 by Richard Robertson Link to comment Share on other sites More sharing options...
Leonick Posted July 9, 2009 Author Share Posted July 9, 2009 My regex did have a \s* after duration. But also, no, I don't regularly test the code I write on here.Sorry after timer i mean....But anyway i learned /s* (very useful)What do you think about my final solution to get the time into the variables? ok?But anyway thx again... Link to comment Share on other sites More sharing options...
Richard Robertson Posted July 10, 2009 Share Posted July 10, 2009 (edited) I do have a comment about your code. How do you know whether 00:00 is 00 hours 00 minutes or 00 minutes 00 seconds? That was part of my problem with writing the original regex. Edited July 10, 2009 by Richard Robertson Link to comment Share on other sites More sharing options...
Authenticity Posted July 10, 2009 Share Posted July 10, 2009 If the data is in a persistent format then seconds are always preceding minutes which are always preceding hours. That is why, in my opinion, the optional non-capturing groups are less restrictive than they should be, i.e. it's possible to match also 99:91:00 things if they are presented in the data. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now