Jump to content

Simple RegExp #2


Recommended Posts

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})?(?:D)?

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

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 by Leonick
Link to comment
Share on other sites

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

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

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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...