Sign in to follow this  
Followers 0
Leonick

How to put a variable in my working RegExp?

9 posts in this topic

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)

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites

You need to use 3 as a parameter if you want an array as a return value. Use ubound() to check the size of the array.


[font="Verdana"] [size="2"]"[/size][/font]Failure is not an option -- it comes packaged with Windows"[font="Verdana"][size="2"] Gecko Web Browser[/size][/font][font="Verdana"][size="2"], [/size][/font][font="Verdana"][size="2"]Yahtzee![/size][/font][font="Verdana"][size="2"], Toolbar Launcher (like RocketDock)[/size][/font][font="Verdana"][size="2"]Internet Blocker, Simple Calculator, Local Weather, Easy GDI+ GUI [/size][/font][font="Verdana"][size="2"]Triangle Solver, TCP File Transfer, [/size][/font][font="Verdana"][size="2"]Valuater's Autoit Wrappers[/size][/font][font="Verdana"][size="3"][size="2"][size="2"]OOP In AutoIt[/size][/size][/size][/font][font="Verdana"][size="2"][size="1"]Using Windows XP SP3, 1GB RAM, AMD Athlon Processor @ 2.1 GHzCheck me out at gadgets.freehostrocket.com[/size][/size][/font]

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

..or to wrap the interpolated string in a \Q...\E block and don't forget (?x) because it seems like a commented pattern.

Share this post


Link to post
Share on other sites

..or to wrap the interpolated string in a \Q...\E block

You know, you learn something new everyday (especially when it comes to RegExp!)

Thanks Authenticity.

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

$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

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

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

Share this post


Link to post
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
Sign in to follow this  
Followers 0