Realm Posted April 30, 2010 Posted April 30, 2010 (edited) I will try to explain what I need best I can, lets say $string = "Hello @visitor, last time you visited us, you were reading @page, what topic would you like to read about today?" I need to convert this string to $string = "Hello" & $visitor & ", last time you visited us, you were reading" & $page & "what topic would you like to read about today?" I was looking at StringRegExpReplace, but could not figure out how to get it the way i need it I'm reading the first string from a txt file, so if it is easier, or possible, to modify the txt file first, that is ok too. NVM I found a fix, but is there a better way than this? $string = "Hello @visitor, last time you visited us, you were reading @page, what topic would you like to read about today?" $string = StringReplace($string, '@visitor', ' " & $visitor & "') $string = StringReplace($string, '@page', ' " & $page & "') MsgBox(0, "New string is", $string) Edited April 30, 2010 by Realm My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.
Moderators Melba23 Posted April 30, 2010 Moderators Posted April 30, 2010 Realm,My attempt at an SRE: $sText = '"Hello @visitor, last time you visited us, you were reading @page, what topic would you like to read about today"' $sNewtext = StringRegExpReplace(StringRegExpReplace($sText, "(?i)(?U)(\x20@visitor)", '" & $visitor & "'), "(?i)(?U)(\x20@page)", '" & $page & "') MsgBox(0, "Changed", $sNewtext)OK, I see you have found a way with StringReplace - normally SREs are much faster than the String* functions, but a lot depends on the size of your file. And whichever way you choose, I would recommend reading in the whole file, changing the text and then rewriting it. Doing it line-by-line will probably take longer, and be more error-prone.M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Fulano Posted April 30, 2010 Posted April 30, 2010 Here's my take on it (I lifted everything but the regex from Melba ) $sText = '"Hello @visitor, last time you visited us, you were reading @page, what topic would you like to read about today"' $sNewtext = StringRegExpReplace($sText, "@([A-z0-9]+)", '" & $\1 & "') MsgBox(0, "Changed", $sNewtext) #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!
Realm Posted April 30, 2010 Author Posted April 30, 2010 (edited) Ok I tried this, but its not working, what am I doing wrong? this is 3 samples from my ini [Messages] welcome=Hello @visitor, last time you visited us, you were reading @page, what topic would you like to read about today? thanks=Thank you for buying @book. @visitor, Come back anytime we are open 24/7. buybalmsg=@visitor, your balance is: @buynumber Gold. this is a snippet i coded to just read them and see how it works, but it don't $var = IniReadSection("C:\Temp\myfile.ini", "Messages") If @error Then MsgBox(4096, "", "Error occurred, probably no INI file." Else $sNewtext = StringRegExpReplace(StringRegExpReplace($var, "(?i)(?U)(\x20@visitor)", '" & $visitor & "'), "(?i)(?U)(\x20@page)", '" & $page & "', & _ & "(?i)(?U)(\x20@book)", '" & $book & "', "(?i)(?U)(\x20@buynumber)", '" & $buynumber & "') For $i = 1 To $sNewtext[0][0] MsgBox(4096, "", "Key: " & $sNewtext[$i][0] & @CRLF & "Value: " & $sNewtext[$i][1]) Next EndIf What I ultimately need this to do is to read all the messages into Global variables, so they can be called randomly throught the script. The Global variables are the same as the in keys such as ini file key 'Welcome' = Global variable $welcome Sorry the error i get is in the StringRegExpReplace, says error in expresion Edited April 30, 2010 by Realm My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.
Moderators Melba23 Posted April 30, 2010 Moderators Posted April 30, 2010 Fulano, I am so glad there are people around here who actually understand these blasted things! As I have said many times before (and no doubt will many times more) these RegExes are the hardest thing I have ever tried to learn*. M23 * With regard to computers, of course. Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Realm Posted April 30, 2010 Author Posted April 30, 2010 Fulano,I am so glad there are people around here who actually understand these blasted things! As I have said many times before (and no doubt will many times more) these RegExes are the hardest thing I have ever tried to learn*.M23* With regard to computers, of course. I Second that motion My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.
Moderators Melba23 Posted April 30, 2010 Moderators Posted April 30, 2010 Realm,Our posts crossed.You cannot just add different parts to the StrngRegExpReplace like that. I see you are using an ini file - that simplifies things a bit. So, stealing from Fulano (I just know he will not mind! ):$sIniFile = "Realm.ini" ; this is the text you gave us above $aLines = IniReadSection($sIniFile, "Messages") For $i = 1 To $aLines[0][0] $aLines[$i][1] = StringRegExpReplace($aLines[$i][1], "@([A-z0-9]+)", '" & $\1 & "') Next IniWriteSection($sIniFile, "Messages", $aLines)I think that will do the trick. M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
Fulano Posted April 30, 2010 Posted April 30, 2010 Np, just takes a special kind of crazy #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!
Realm Posted April 30, 2010 Author Posted April 30, 2010 Fulano, thanks, I got it now Global $welcome = IniRead("C:\Temp\myfile.ini", "Messages", "welcome", "NotFound") Global $thanks = IniRead("C:\Temp\myfile.ini", "Messages", "thanks", "NotFound") Global $buybalmsg = IniRead("C:\Temp\myfile.ini", "Messages", "welcome", "NotFound") $sNewtext = StringRegExpReplace($welcome , "@([A-z0-9]+)", '" & $\1 & "') MsgBox(0, "Changed", $sNewtext) $sNewtext = StringRegExpReplace($thanks , "@([A-z0-9]+)", '" & $\1 & "') MsgBox(0, "Changed", $sNewtext) $sNewtext = StringRegExpReplace($buybalmsg , "@([A-z0-9]+)", '" & $\1 & "') MsgBox(0, "Changed", $sNewtext) works like a charm, and not as messy as i thought it would end up My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.
Moderators Melba23 Posted April 30, 2010 Moderators Posted April 30, 2010 Realm, But you have not seen my last post yet! M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area
jchd Posted April 30, 2010 Posted April 30, 2010 AutoItSetOption('ExpandVarStrings', 1) It won't work with @xyz (reserved for macros) but will work for (flat) variables. $visitor = 'Realm' $page = 'http://www.autoitscript.com/autoit3/docs/functions/AutoItSetOption.htm' $string = "Hello $visitor, last time you visited us, you were reading $page what topic would you like to read about today?" will evaluate the way you whish without fuss. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
Spiff59 Posted April 30, 2010 Posted April 30, 2010 (edited) I wonder if you might be better off using something like "@@" as your keyword delimiter in case you ever want to include an actual "@" in the text of your messages (for instance, an email address). Edited April 30, 2010 by Spiff59
Fulano Posted April 30, 2010 Posted April 30, 2010 I wonder if you might be better off using something like "@@" as your keyword delimiter in case you ever want to include an actual "@" in the text of your messages (for instance, an email address).Good point! #fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!
Realm Posted April 30, 2010 Author Posted April 30, 2010 Realm, Our posts crossed. You cannot just add different parts to the StrngRegExpReplace like that. I see you are using an ini file - that simplifies things a bit. So, stealing from Fulano (I just know he will not mind! ): $sIniFile = "Realm.ini" ; this is the text you gave us above $aLines = IniReadSection($sIniFile, "Messages") For $i = 1 To $aLines[0][0] $aLines[$i][1] = StringRegExpReplace($aLines[$i][1], "@([A-z0-9]+)", '" & $\1 & "') Next IniWriteSection($sIniFile, "Messages", $aLines) I think that will do the trick. M23 Your Right I did'nt see it, I tested it, I like it, but I will change the iniwrite to another file location/name, so that it does not conflict with the config script that modifies it Thanks My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.
jchd Posted April 30, 2010 Posted April 30, 2010 Are you bound with @ character or can you use the ExpandVars option? This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
Realm Posted May 1, 2010 Author Posted May 1, 2010 I wonder if you might be better off using something like "@@" as your keyword delimiter in case you ever want to include an actual "@" in the text of your messages (for instance, an email address).That is a wonderful suggestion, thanks My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.
Realm Posted May 1, 2010 Author Posted May 1, 2010 (edited) Are you bound with @ character or can you use the ExpandVars option?Unfortunately from which the information comes is declared only with @variables.I'm trying to keep it simple for the game module I'm creating for a teacher, which will allow her students, end users, to make some of the game themselves using allready declared variables, she wants them to be easy like @variable.Unless you have a better idea that will keep it simple for her elementary level students? Edited May 1, 2010 by Realm My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.
jchd Posted May 1, 2010 Posted May 1, 2010 Then can you replace @ by $ and name your variables accordingly. The expansion will be automatic with this option.. This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe hereRegExp tutorial: enough to get startedPCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta. SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)
Realm Posted May 1, 2010 Author Posted May 1, 2010 (edited) Then can you replace @ by $ and name your variables accordingly. The expansion will be automatic with this option..Unfortunately the teacher hired someone to create this entire job. they wrote a working config file, but the game module does not work at all. the config file is compiled, so I would have to write a custom config file for here, and still have to reformat the string.$string = "Hello $visitor, last time you visited us, you were reading $page what topic would you like to read about today?"$string = "Hello" & $visitor & ", last time you visited us, you were reading" & $page & "what topic would you like to read about today?"At this point seems, easier to just work the game module to reformat the current output which inclueds the @variables format.I do appreciate the suggestion, it would be better and easier, if I could modify the config file Edited May 1, 2010 by Realm My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.
Spiff59 Posted May 6, 2010 Posted May 6, 2010 At this point seems, easier to just work the game module to reformat the current output which inclueds the @variables format.I do appreciate the suggestion, it would be better and easier, if I could modify the config fileIf you're stuck with this format, then I might add a space to each side of the StringRegExReplace(), so that you are matching on " @" instead of just "@", that would make for a more bullet-proof script that would permit strings like "me@home.com" to exist in the text of the message.
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