Cyri Posted December 10, 2009 Posted December 10, 2009 I have some files that resemble an INI file, but they don't have any sections. It's basically one big file looking like this... X=1 Y=2 Blah=false The built-in INI functions of AutoIT don't work when there's no section name. Is there an easy way to pull out these values or am I going to have to loop through and do a readline through the file? Then do some string manipulation based on the equals sign.
Mat Posted December 10, 2009 Posted December 10, 2009 (edited) Use StringRegExp StringRegExp (FileRead ($sFile), "(?i)\Q" & $sKey & "\E=(.*)", 1) The result will be in the 0th element. Mat Edit: Made it case insensitive. Edited December 10, 2009 by Mat AutoIt Project Listing
Cyri Posted December 10, 2009 Author Posted December 10, 2009 Use StringRegExp StringRegExp (FileRead ($sFile), "(?i)\Q" & $sKey & "\E=(.*)", 1) The result will be in the 0th element. Mat Edit: Made it case insensitive. Looks like that may work for what I need. Question: why does it only return the first match? The flag = 1 on the regex should return an array of matches but it doesn't seem to work. Also, is there a way to strip out the @CR in the StringRegExp matches so I don't have to strip them out afterwards?
whim Posted December 10, 2009 Posted December 10, 2009 Why not just stick a dummy [whatever] in front of it ? (cheating, but effective ) whim
Cyri Posted December 10, 2009 Author Posted December 10, 2009 Why not just stick a dummy [whatever] in front of it ? (cheating, but effective ) whim In front of what exactly? I have no control over the values that get written into the file.
Mat Posted December 10, 2009 Posted December 10, 2009 I presumed you only wanted the first... Change the flag to 3. Func _IniLikeRead ($sFile, $sKey, $sDefault = "") Local $asRet = StringRegExp (FileRead ($sFile), "(?:\n|\n|\f|\a)\Qblah\E=(.*?)(?:\r|\n|\f|\z)", 3), $sRet = "|" If @Error Then Return SetError (1, 0, $sDefault) For $i = 0 to UBound ($asRet) - 1 If StringinStr ($sRet, "|" & $asRet[$i] & "|") Then ContinueLoop $sRet &= $asRet[$i] & "|" Next Return SetExtended ($i, StringTrimLeft (StringTrimRight ($sRet, 1), 1)) EndFunc ; ==> _IniLikeRead AutoIt Project Listing
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