Followers 0

StringRegExpReplace - Simple Replace

4 posts in this topic

Morning

It's 5:30 AM and been sitting here staring at the help file but it looks like an alien language to me.

I have the following example strings

[8]Bird
[9]Cat
[10]Dog

What I want is

Bird
Cat
Dog

Now a easy solution would be to just trim it to the left by 3, however there are cases when it could be 4 (example Dog being [10]), so I figured StringRegExpReplace() is what I am going to have to use unless there is a easier/better method.

I don't know how or where to start though

StringRegExpReplace($String, "[*]", "") Thanks Share this post Link to post Share on other sites StringRegExpReplace($String, "[d+]", "")

1 person likes this

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
RegExp tutorial: enough to get started
PCRE 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)

Share on other sites

Thanks, ill try that when I wake up a little more. Seems to be just what I need though

Create an account

Register a new account

Func _MyInc () Static Local $i = 0$i += 1 Return $i EndFunc Exit _InsertLines() Func _InsertLines()$String = "A" & @CRLF & "B" & @CRLF & "C" & @CRLF & "D" $NewString = Execute("'" & StringRegExpReplace($String,"[\r\n]*",  "' & _MyInc () & '\1" ) & "'") MsgBox (0, "", $NewString) EndFunc but I get this: 1A23B45C67D8 I never really could master how Execute works here and I always get some working example and make substitutions. But this is the closest i could get... • By AutoBert The idea to use translation api: i used the script from @mikell to build this func: Func _Translate($sFrom, $from,$to) ;thanks to mikell (autoitscript.com) ;https://www.autoitscript.com/forum/topic/182893-prompt-me-how-to-see-the-text-in-the-translation-boxhttpstranslategooglecom/?do=findComment&comment=1313423 Local $url = "https://translate.googleapis.com/translate_a/single?client=gtx"$url &= "&sl=" & $from & "&tl=" &$to & "&dt=t&q=" & $sFrom Local$oHTTP = ObjCreate("Microsoft.XMLHTTP") $oHTTP.Open("POST",$url, False) $oHTTP.Send() Local$sData = $oHTTP.ResponseText$sData = StringRegExpReplace($sData, '.*?$"(.*?)"[^\[]*', "1" & @CRLF) Return sData EndFunc ;==>_Translate when i call this func with: sText='AutoIt v3 is a freeware BASIC-like scripting language designed for automating the Windows GUI and general scripting. It uses a combination of simulated keystrokes, mouse movement and window/control manipulation in order to automate tasks in a way not possible or reliable with other languages (e.g. VBScript and SendKeys). AutoIt is also very small, self-contained and will run on all versions of Windows out-of-the-box with no annoying "runtimes" required!' MsgBox(64,'',_Translate(sText,'en','de')) nearly all is seeing here: only the "!" is wrong "\" but when using 'auto' instead of 'en' the result is: 2 lines are appended. So my question is, is it possible to extend the pattern (i never worked with regex) and in best case setting @extended with the detected language? @Trong: as you can see yet i am returning translated text and don't use GuiCtrlSetData to assign it to a EditBox. • By TheDcoder Hello! I am trying to call my first DLL function but it won't work , I am trying to call GetFinalPathNameByHandle sFile = @DesktopDir & '\Test.txt' hFile = FileOpen(sFile) sFilePath = "" DllCall("Kernel32.dll", "STR", "GetFinalPathNameByHandle", "HANDLE", hFile, "sz*", sFilePath) MsgBox(0, 0, @error) Thanks in Advance, TD • By ViciousXUSMC I was working on something last night and decided to use StringRegExpReplace() for a config file, I never noticed that you cant just "overwrite" the file with the update so easily it required a few more pieces of code to work properly. Is this the simplest way (what I used) and while I searched for it and did not find it do we have or will we have a RegEx equivalent for _ReplaceStringInFile()? sFile = FileRead(@ScriptDir & "\test.txt") hFile = FileOpen(@ScriptDir & "\test.txt", 2) sNewContent = StringRegExpReplace(sFile, "(test)", "new1") FileWrite(hFile, sNewContent) FileClose(hFile) • By Mecano Hallo Members, I'm looking for a good regex to get the drive letter and the last two folders from a file path, If the path is to long for the label width then show drive + ellipses and two last folders. Drive:\(ellipses)\folder\folder ex. D:\...\folder\folder and when the folder is in the root of the drive then show D:\Folder The test GUI #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.12.0 Author: Mecano Script Function: ELLIPSIS Long path: Drive:\...\Folder\Folder if root then Drive:\Folder #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here sFile = "F:\Just a folder\in a another folder\and another\This-dir\And-this-dir" EllipsisPath = StringRegExpReplace(sFile, '\w[a-zA-Z \$+\\', '') ; <- This needs another regex ;no ellipsis needed, for testing purposes only$sUSB = "K:\Just a folder" $PathforUSB = StringRegExpReplace($sUSB, '\w[a-zA-Z \\]+\\', '') #include <ButtonConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> $Form1 = GUICreate("Drive + Two last folders of path", 327, 236, 192, 124)$Button1 = GUICtrlCreateButton("Test label", 40, 168, 97, 33) $Label1 = GUICtrlCreateLabel("F:\...\This-dir\And-this-dir", 40, 12, 200, 40) ; <- Looks good but not dynamic GUICtrlSetColor($Label1, 32768) $Label2 = GUICtrlCreateLabel($sFile, 40, 40, 200, 40, $DT_END_ELLIPSIS) ; <- not the last two directorys GUICtrlSetColor($Label2, 16711680) $Label3 = GUICtrlCreateLabel("Var label1", 40, 72, 200, 40) GUICtrlSetColor($Label3, 16711680) $Label4 = GUICtrlCreateLabel("Var label", 40, 104, 200, 40) GUICtrlSetColor($Label4, 16711680) GUISetState(@SW_SHOW) While 1 $nMsg = GUIGetMsg() Switch$nMsg Case $GUI_EVENT_CLOSE Exit Case$Button1 GUICtrlSetData($Label3,$EllipsisPath) ; $DT_END_ELLIPSIS <- works only on GUICtrlCreateLabel GUICtrlSetData($Label4, \$PathforUSB) EndSwitch WEnd