Champak Posted April 6, 2022 Share Posted April 6, 2022 (edited) I have a string to search for 4 different things, with four different results $sText = StringReplace($sText2, "'", "") $sText = StringReplace($sText, "/", "-") $sText = StringReplace($sText, " ", "_") $sText = StringReplace($sText, "&", "And") Can all three be combined into one regexp? Thanks Edited April 6, 2022 by Champak Link to comment Share on other sites More sharing options...
ad777 Posted April 6, 2022 Share Posted April 6, 2022 maybe this what your looking for: $st = './ ' $out = StringRegExpReplace($st, '(?m)..','-_',1) iam ِAutoit programmer. best thing in life is to use your Brain to Achieve everything you want. Link to comment Share on other sites More sharing options...
Developers Jos Posted April 6, 2022 Developers Share Posted April 6, 2022 (edited) @ad777 mmm not even close to what is asked! A shame the OP didn't post a runnable replicator script, but that was easy in this case: $sText = " this is a test ' / $ " $sText = StringReplace($sText, "'", "") $sText = StringReplace($sText, "/", "-") $sText = StringReplace($sText, " ", "_") $sText = StringReplace($sText, "&", "And") ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sText = ' & $sText & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console $sText = " this is a test ' / $ " $sText = StringRegExpReplace($sText, '(?m)..','-_',1) ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $sText = ' & $sText & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console @Champak, I normally make array that contains the from & to replacement and loop through the array doing a stringreplace() Edited April 6, 2022 by Jos SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
Champak Posted April 6, 2022 Author Share Posted April 6, 2022 (edited) Thanks. However, neither work. $sText = "Jack's birthday is 02/02/02 & Jill's is the same" $sText = StringRegExpReplace($sText, '(?m)..','-_',1) ConsoleWrite($sText) This is what the result should be: "Jacks_birthday_is_02-02-02_And_Jills_is_the_same" Putting stringreplace in an array is a good idea, I never thought about that. I'll give it a try, but the reason I wanted it with stringregexp is I thought 1/ it would be faster, and 2/ I'd prefer it inline with the rest of this particular passage of code I have. Edited April 6, 2022 by Champak Link to comment Share on other sites More sharing options...
Developers Jos Posted April 6, 2022 Developers Share Posted April 6, 2022 3 minutes ago, Champak said: However, neither work. Isn't that what I already stated? SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past. Link to comment Share on other sites More sharing options...
mikell Posted April 6, 2022 Share Posted April 6, 2022 1 hour ago, Champak said: 2/ I'd prefer it inline with the rest So just build a func (using Jos' method or not) $txt = "Jack's birthday is 02/02/02 & Jill's is the same" Msgbox(0,"", _Job($txt) ) ;---------------------- Func _Job($sText) $sText = StringReplace($sText, "'", "") $sText = StringReplace($sText, "/", "-") $sText = StringReplace($sText, " ", "_") $sText = StringReplace($sText, "&", "And") Return $sText EndFunc Regex is magic but unfortunately it has limits How frustrating Link to comment Share on other sites More sharing options...
pixelsearch Posted April 6, 2022 Share Posted April 6, 2022 This reminds me the old Foxpro days (with the following example just found in this link) where replacing several characters at a time was doable like this : ? CHRTRAN("ABCDE", "ACE", "XYZ") && yields "XBYDZ" Memories, memories... Danp2 1 Link to comment Share on other sites More sharing options...
jchd Posted April 6, 2022 Share Posted April 6, 2022 When you have a fix translate table and need to use it many times, you could make it this way: Local $mXlate[] $mXlate["A"] = "Z" $mXlate["B"] = "Y" $mXlate["C"] = "X" $mXlate["D"] = "W" $mXlate["E"] = "V" $mXlate["F"] = "U" $mXlate["U"] = "F" $mXlate["V"] = "E" $mXlate["W"] = "D" $mXlate["X"] = "C" $mXlate["Y"] = "B" $mXlate["Z"] = "A" Local $sOut = Execute("'" & StringRegExpReplace("ABCDEF...UVWXYZ", "([A-FU-Z])", "' & $mXlate['$1'] & '") & "'") ConsoleWrite($sOut & @LF) ad777 1 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) Link to comment Share on other sites More sharing options...
Deye Posted April 8, 2022 Share Posted April 8, 2022 (edited) For kicks, One overall working example $txt = "Jack's birthday is 02/02/02 & Jill's is the same" MsgBox(0, '', _Replace($txt, "'/ &" & @TAB & "|-|_|And")) MsgBox(0, '', _Replace($txt, "'/ &02" & @TAB & "| \ | |And|12|34")) MsgBox(0, '', _Replace($txt, "/&20" & @TAB & "1|may be||")) Func _Replace($sTxt, $sSplit) Local $aSplit = StringSplit($sSplit, @TAB) Local $a1 = StringSplit($aSplit[1], "") Local $a2 = StringSplit($aSplit[2], "|") Local $sda = ObjCreate("Scripting.Dictionary") For $i = 1 To UBound($a1) - 1 $sda.item($a1[$i]) = $a2[$i] Next Return Execute('"' & StringRegExpReplace($sTxt, "([" & $aSplit[1] & "])", '" & $sda.Item("$1") & "') & '"') EndFunc Edited April 10, 2022 by Deye Link to comment Share on other sites More sharing options...
Champak Posted April 10, 2022 Author Share Posted April 10, 2022 @Deye, that's very cool. Been staring at it for 20 min and can't figure it out. I went with mikell's suggestion, I don't know why I forgot I could do that. Thanks all. Link to comment Share on other sites More sharing options...
Deye Posted April 10, 2022 Share Posted April 10, 2022 Updated the above to make it easier to work with + examples. Link to comment Share on other sites More sharing options...
rudi Posted April 13, 2022 Share Posted April 13, 2022 @jchd This code looks very interesting, but I don't really get it ( local dimming an array without giving the arrays size, filling later on with index addressing using one-letter-strings) Local $mXlate[] $mXlate["A"] = "Z" $mXlate["B"] = "Y" $mXlate["C"] = "X" $mXlate["D"] = "W" $mXlate["E"] = "V" $mXlate["F"] = "U" $mXlate["U"] = "F" $mXlate["V"] = "E" $mXlate["W"] = "D" $mXlate["X"] = "C" $mXlate["Y"] = "B" $mXlate["Z"] = "A" Local $sOut = Execute("'" & StringRegExpReplace("ABCDEF...UVWXYZ", "([A-FU-Z])", "' & $mXlate['$1'] & '") & "'") ConsoleWrite($sOut & @LF) Could you pls. drop some explaining lines for it? TIA, Rudi. Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
jchd Posted April 13, 2022 Share Posted April 13, 2022 This is a Map datatype, not an array. More or less an equivalent to a Scripting.Dictionary object (an indexed key-value store), but Maps are built-in beta version of AutoIt. See beta help on Variables > Maps & Arrays > Maps. 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) Link to comment Share on other sites More sharing options...
rudi Posted April 13, 2022 Share Posted April 13, 2022 I see, very interesting! Is this an equivalent to the powershell hash table variable type? Especially the aspect of access speed? In powershell even very large hash tables return the test // value results literally instantly. Earth is flat, pigs can fly, and Nuclear Power is SAFE! Link to comment Share on other sites More sharing options...
jchd Posted April 13, 2022 Share Posted April 13, 2022 Exactly IIRC. Since in these type of containers the indexing is done and balanced on the fly as new entries are inserted, the access is O(log₂ N). 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) Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted April 13, 2022 Moderators Share Posted April 13, 2022 8 hours ago, jchd said: Maps are built-in beta version of AutoIt Maps are now available in the latest release - 3.3.16.0. 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 Link to comment Share on other sites More sharing options...
jchd Posted April 13, 2022 Share Posted April 13, 2022 Ah yes, I always hesitate between beta and release. My bad. 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) Link to comment Share on other sites More sharing options...
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