Jump to content

How to put a variable in my working RegExp?


Recommended Posts

Hey all.

Since the help of the forum(Big thx) i managed to learn the basics of regexp.

But my Problem now is, i wanna replace the word "timer1:" with a variable...tried many things, but not working :-(

#include <Array.au3>

global $hours,$min,$sec

$data = "Timer1:        duration:     1d  03: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

String concatenation? or a special feature - AutoIt string content interpolation?

#include <Array.au3>
Opt("ExpandVarStrings", 1)

Global $hours,$min,$sec
Global $sVar = 'Timer1:'

$data1 = "Timer1:        duration:     1d  03:18:09"
$data2 = $sVar & "        duration:     1d  03:18:09"
$data3 = "$sVar$        duration:     1d  03:18:09"

...if I get what you mean. :)

Edit: By the way, it'd be clever to check if an array's size is matching your expectation before trying to access it's subscripts which may not happen to exists.

Edited by Authenticity
Link to comment
Share on other sites

And now i dont really get what YOU mean :rolleyes:

But i dont wanna punt a variable into my $data, i wanna put the variable into my regexp

I Just wanna replace the word "timer1:" in this line:

$time = StringRegExp($data, "(?i)timer1:\s*duration:\s*(? :( \d{1,2})d\s*)?(? :( \d{2}) :) ?(? :( \d{2}) :) ?(\d{2})?", 1)

Edited by Leonick
Link to comment
Share on other sites

And now i dont really get what YOU mean :rolleyes:

But i dont wanna punt a variable into my $data, i wanna put the variable into my regexp

I Just wanna replace the word "timer1:" in this line:

$time = StringRegExp($data, "(?i)timer1:\s*duration:\s*(? :( \d{1,2})d\s*)?(? :( \d{2}) :) ?(? :( \d{2}) :) ?(\d{2})?", 1)

$var = "timer1"
$time = StringRegExp($data, "(?i)" & $var & ":\s*duration:\s*(? :( \d{1,2})d\s*)?(? :( \d{2}) :) ?(? :( \d{2}) :) ?(\d{2})?", 1)

Beware that if your variable has a space or any other character that has a special meaning in RegExp, then you will have to substitute in the required RegExp equivalent or escape it first.

Link to comment
Share on other sites

$var = "timer1"
$time = StringRegExp($data, "(?i)" & $var & ":\s*duration:\s*(? :( \d{1,2})d\s*)?(? :( \d{2}) :) ?(? :( \d{2}) :) ?(\d{2})?", 1)

Beware that if your variable has a space or any other character that has a special meaning in RegExp, then you will have to substitute in the required RegExp equivalent or escape it first.

@ResNullius

NOPE doesn't work.

But anyway, i came to the solution by myself, 15mins after i wrote the topic....lol(just forget to answer, since i was too happy to go on scripting)

THIS would be the exact solution:

$time = StringRegExp($data, "(?i)(?:"&$test&")\s*duration:\s*(? :( \d{1,2})d\s*)?(? :( \d{2}) :) ?(? :( \d{2}) :) ?(\d{2})?", 1)

and the /q.../e thing i dont get.....

how would it look like then?

Edited by Leonick
Link to comment
Share on other sites

Either of the two:

; #1
$time = StringRegExp($data, "(?i)(?:\Q"&$test&"\E)\s*duration:\s*(? :( \d{1,2})d\s*)?(? :( \d{2}) :) ?(? :( \d{2}) :) ?(\d{2})?", 1)

; #2
$test = "\QTimer:\E"
$time = StringRegExp($data, "(?i)(?:"&$test&")\s*duration:\s*(? :( \d{1,2})d\s*)?(? :( \d{2}) :) ?(? :( \d{2}) :) ?(\d{2})?", 1)
Edited by Authenticity
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...