Leonick Posted July 13, 2009 Posted July 13, 2009 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)
Authenticity Posted July 14, 2009 Posted July 14, 2009 (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 July 14, 2009 by Authenticity
dantay9 Posted July 14, 2009 Posted July 14, 2009 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.
Leonick Posted July 14, 2009 Author Posted July 14, 2009 (edited) And now i dont really get what YOU mean 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 July 14, 2009 by Leonick
ResNullius Posted July 14, 2009 Posted July 14, 2009 And now i dont really get what YOU mean 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.
Authenticity Posted July 14, 2009 Posted July 14, 2009 ..or to wrap the interpolated string in a \Q...\E block and don't forget (?x) because it seems like a commented pattern.
ResNullius Posted July 14, 2009 Posted July 14, 2009 ..or to wrap the interpolated string in a \Q...\E blockYou know, you learn something new everyday (especially when it comes to RegExp!)Thanks Authenticity.
Leonick Posted July 14, 2009 Author Posted July 14, 2009 (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 July 14, 2009 by Leonick
Authenticity Posted July 14, 2009 Posted July 14, 2009 (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 July 14, 2009 by Authenticity
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