ethneldryt Posted January 23, 2009 Share Posted January 23, 2009 Hello, I need to remove the space of a variable with StringRegExpReplace or others. I'm bad at regexp Thx exemple : $var = "hello world" result : "helloworld" Link to comment Share on other sites More sharing options...
Manjish Posted January 23, 2009 Share Posted January 23, 2009 Use this: StringRegExpReplace("Hello World", "[ ]", "") [font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com Link to comment Share on other sites More sharing options...
ethneldryt Posted January 23, 2009 Author Share Posted January 23, 2009 It does not work :s Link to comment Share on other sites More sharing options...
Manjish Posted January 23, 2009 Share Posted January 23, 2009 (edited) What r u talking about.. It works perfectly well.. MsgBox(0, "Regular Expression Replace Test", StringRegExpReplace("Hello World", "[ ]", "")) Edited January 23, 2009 by Manjish [font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com Link to comment Share on other sites More sharing options...
Robjong Posted January 23, 2009 Share Posted January 23, 2009 (edited) Hey this should do the trick, $sString = " Hello World!" $sString = StringRegExpReplace($sString, "\s", "") MsgBox(0, "String without spaces", $sString) \s matches any type of space Edited January 23, 2009 by Robjong Link to comment Share on other sites More sharing options...
MrCreatoR Posted January 23, 2009 Share Posted January 23, 2009 Why use RegExp, when you can just use StringStripWS... $sVar = "hello world" $sResult = StringStripWS($sVar, 8) ?  Spoiler Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1  AutoIt Russian Community My Work... Spoiler Projects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize ProgramUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF Examples: ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating ) * === My topics === * ================================================== ==================================================    AutoIt is simple, subtle, elegant. © AutoIt Team Link to comment Share on other sites More sharing options...
betatester Posted February 13, 2009 Share Posted February 13, 2009 i want to replace a double quote " in a script script is a directory path passed to the script as CmdLine[1] so e.g CmdLine[1] is "c:\blah\blue" i want to remove double quote and keep rest of path c:\blah\blue $sStrippedPend1 = StringReplace($CmdLineRaw, "["]", "") or $sStrippedPend1 = StringReplace($CmdLineRaw, """, "") don't work because " is seen as script language (unterminated script) what's the solution?.. thanks Link to comment Share on other sites More sharing options...
betatester Posted February 13, 2009 Share Posted February 13, 2009 used stringtrimright and left to remove double quotes..it worked $line=FileReadLine ($CmdLine[1], 2 ) $sStrippedCmd = StringRegExpReplace($line, "URL=", "") $sStrippedPend1 = StringReplace($CmdLineRaw, "E:\briefcase\", "") $sStrippedPend2 = StringReplace($sStrippedPend1, "HE-pending\", "") $sStrippedPend3 = StringTrimLeft($sStrippedPend2, 1) $sStrippedName = StringTrimRight($sStrippedPend3, 1) Link to comment Share on other sites More sharing options...
stampy Posted February 13, 2009 Share Posted February 13, 2009 a) when running from the command line the parameters already get the quotes striped from them unless there are extra quotes being added b)stringreplace seems more simple unless there are quotes in the param $a='"asdfasdf asdfasdf"' MsgBox(0,'',StringReplace($a,'"','')) Link to comment Share on other sites More sharing options...
verris Posted February 14, 2009 Share Posted February 14, 2009 Ok, so here is an even more n00b question. For the life of me I can't figure out how to replace an arbitrary text between two known strings. I have a text file with multiple lines and know that the text I want to replace starts with string a and ends with string b on a different line further down. I want to replace the text including string a and b. I know this must be trivial, but I just spent the better part of an hour browsing the forums for a solution...Any help is greatly appreciated! Link to comment Share on other sites More sharing options...
Robjong Posted February 14, 2009 Share Posted February 14, 2009 (edited) Hey, this should get you started $sString = "I am a string with some arbitrary " & @CRLF & _ "text that needs to be replaced, " & @CRLF & _ "this part needs to remain in place." & @CRLF $sString = StringRegExpReplace($sString, "(?i)(?s)(.*)arbitrary.*?replaced(.*)", "\1new text\2") ConsoleWrite($sString & @CRLF) "(?i)(?s)(.*)arbitrary.*?replaced(.*)" (?i) - case insensetive (?s) - . also matches new lines (.*) - captures the text from beginning up to "arbitrary" arbitrary- the known word the string we want to replace starts with .* - matches anything (including newlines) ? - tells the engine to get the smallest match instead of the largest (if replaced would be in the text more then once after the starting word the first one is used) replaced- the known word the string we want to replace ends with (.*) - captures the text from "replaced" to the end "\1new text\2" \1 - backreference to the first captured text new text - our new replacing text \2 - backreference to the second captured text Edited February 14, 2009 by Robjong Link to comment Share on other sites More sharing options...
Malkey Posted February 14, 2009 Share Posted February 14, 2009 Ok, so here is an even more n00b question. For the life of me I can't figure out how to replace an arbitrary text between two known strings. I have a text file with multiple lines and know that the text I want to replace starts with string a and ends with string b on a different line further down. I want to replace the text including string a and b. I know this must be trivial, but I just spent the better part of an hour browsing the forums for a solution...Any help is greatly appreciated!And this appears to work also. Local $sFileName = "Path\NameOfFile.txt" Local $sStrA = "first string" Local $sStrB = "second string" Local $sStrReplace = "HERE IS THE REPLACEMENT STRING" Local $sREResult = StringRegExpReplace(FileRead($sFileName), "(?s)" & $sStrA & "(.*)" & $sStrB, $sStrReplace) ; Saves to same file or could save to another file if required. $hFile = FileOpen($sFileName, 2) FileWrite($hFile, $sREResult) FileClose($hFile) Link to comment Share on other sites More sharing options...
Robjong Posted February 14, 2009 Share Posted February 14, 2009 (edited) Keep in mind that for both examples you will need to make sure you escape any regex control characters, such as . ?(detailed info in the help file for StringRegExp, http://www.autoitscript.com/autoit3/docs/f...ringRegExp.htm)Edit: fixed URL Edited February 14, 2009 by Robjong Link to comment Share on other sites More sharing options...
verris Posted February 14, 2009 Share Posted February 14, 2009 Guys, you are amazing. Thanks so much, you saved my weekend. BTW: Can you point me towards a good tutorial for StringRegExpReplace, if possible one with example scripts, I'm afraid I couldn't really wrap my head around the rather abstract description in the AutoIt help file. Link to comment Share on other sites More sharing options...
Robjong Posted February 14, 2009 Share Posted February 14, 2009 (edited) Hey,There is a link with detailed information in the StringRegExp help info, http://www.autoitscript.com/autoit3/pcrepattern.htmlNot sure if there is more about StringRegExpReplace but, AutoIt uses PCRE (Perl Compatible Regular Expressions) so a search for "PCRE tutorial" will get you started http://www.google.com/search?q=PCRE+tutorialits not that difficult, so i suggest you just open up some tutorial/documentation and play with it a bitEdit: rephrased Edited February 14, 2009 by Robjong Link to comment Share on other sites More sharing options...
verris Posted February 15, 2009 Share Posted February 15, 2009 Thanks, as usual the first google hit helped a lot, if anyone else looks for the same: http://www.regular-expressions.info/quickstart.htmlHowever, I have now hit the next wall: I have a text file in which various information is grouped for different entries, but not every entry has all the information, egfile 1application date 19/03/2003recordal date 20/03/2003file 2application date 30/12/2007file 3application date 10/02/2005recordal date 20/10/2005I need to get this into a csv file. I have tried something along the following lines using ? to make the whole recordal thing optional, which did not work (found no matches):$text = StringRegExpReplace($text, "(?i)(?s)application date (\d\d\.\d\d.\d\d\d\d).*?(recordal date (\d\d\.\d\d.\d\d\d\d))?", "\1,\3")And being at it: Is there a possibility to name groups like in Perl (?P<name>group), I couldn't seem to make that work. Thanks a lot for your help! Link to comment Share on other sites More sharing options...
Authenticity Posted February 15, 2009 Share Posted February 15, 2009 (edited) Can you give more information about what file, for example, might contain other than generic "file1".All I can think of right now is that there is no need for regular expression, only FileRead and StringSplit.Edit: #include <Array.au3> $s = 'The the' $a = StringRegExpReplace($s, '(?i)(?P<NAME>\w+) \b(?P=NAME)\b', '$1') ConsoleWrite(@error & @TAB & @extended & @CRLF & $a & @CRLF)As long as it's a valid pattern I see it does support naming groups. Edited February 15, 2009 by Authenticity Link to comment Share on other sites More sharing options...
verris Posted February 15, 2009 Share Posted February 15, 2009 (edited) Can you give more information about what file, for example, might contain other than generic "file1". All I can think of right now is that there is no need for regular expression, only FileRead and StringSplit. Edit: #include <Array.au3> $s = 'The the' $a = StringRegExpReplace($s, '(?i)(?P<NAME>\w+) \b(?P=NAME)\b', '$1') ConsoleWrite(@error & @TAB & @extended & @CRLF & $a & @CRLF) As long as it's a valid pattern I see it does support naming groups. Sorry, bad description on my part. It is not actually a separate file, it is just a couple of lines in one big file. Where it says "file 1", read "entry 1". Thanks for the naming groups description. i wonder what I did wrong, I'll work it out. Edited February 15, 2009 by verris Link to comment Share on other sites More sharing options...
Authenticity Posted February 15, 2009 Share Posted February 15, 2009 #include <Array.au3> $s = 'file 1' & @CRLF & _ 'application date 19/03/2003' & @CRLF & _ 'recordal date 20/03/2003' & @CRLF & _ 'file 2' & @CRLF & _ 'application date 30/12/2007' & @CRLF & _ 'Some non catched text' & @CRLF & _ 'file 3' & @CRLF & _ 'application date 10/02/2005' & @CRLF & _ 'recordal date 20/10/2005' Dim $sPattern = '(file[^\r\n]*\r\n)(application[^\r\n]*\r\n)?(recordal[^\r\n]*\r\n)?' Dim $aResults = StringRegExp($s, $sPattern, 3) If IsArray($aResults) Then _ArrayDisplay($aResults) Link to comment Share on other sites More sharing options...
Robjong Posted February 15, 2009 Share Posted February 15, 2009 Hey, this might be easyer #include <Array.au3> $sString = "file 1" & @CRLF & "application date 19/03/2003" & @CRLF & "recordal date 20/03/2003" & @CRLF & _ "file 2" & @CRLF & "application date 30/12/2007" & @CRLF & "Some non catched text" & @CRLF & _ "file 3" & @CRLF & "application date 10/02/2005" & @CRLF & "recordal date 20/10/2005" $aRes = StringRegExp($sString, "(?i)((?:file|recordal|application)[^\r\n]+)", 3) _ArrayDisplay($aRes) 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