martin Posted May 6, 2008 Share Posted May 6, 2008 The code below shows what I am trying to do, and the comments are intended to explain it. I am trying to improve my understanding of RegExpr which is not at all good, but the homework I gave myself is too difficult! $t = '' $k = "abcdef ()" For $n = 1 To 10 $t &= $k & $n & @CRLF Next $q = "abcdef" ;look for a string abcdef either at the beginning of the text or at the start of a new line, ;with any number of horizontal white space chars before it, ;any number of horizontal white space chars after, then an open bracket, ;then any number of characters up to a line feed. ;Replace with "xyz" ;this works as I would expect $sText = StringRegExpReplace($t, "(?i)(?:\A|\n)\h*\Q" & $q & "\E\h*\(\V*", 'xyz') MsgBox(0, "result = ", $sText) ;similar but I want to remove the whole line including the CR LF at the end ;I can't get this to work, this is just one of my attempts $sText = StringRegExpReplace($t, "(?i)(?:\A|\n)\h*\Q" & $q & "\E\h*\(.*\r\n", '') MsgBox(0, "result = ", $sText);not all lines are removed. I can explain why not but I can't see how to get round it. Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
Siao Posted May 6, 2008 Share Posted May 6, 2008 ;not all lines are removed. I can explain why not but I can't see how to get round it.With "\n" in the end of your pattern, you are telling to erase the linefeed which belongs to the next line. Therefore the next line does not match the pattern, and is not replaced. Then the second next line is matched again, and the cycle repeats. Therefore you get replacements in odd numbered lines only. I don't think you really need that last "\n" in your pattern. "be smart, drink your wine" Link to comment Share on other sites More sharing options...
martin Posted May 7, 2008 Author Share Posted May 7, 2008 With "\n" in the end of your pattern, you are telling to erase the linefeed which belongs to the next line. Therefore the next line does not match the pattern, and is not replaced. Then the second next line is matched again, and the cycle repeats. Therefore you get replacements in odd numbered lines only. I don't think you really need that last "\n" in your pattern.Yes, but if I remove the \n from the pattern then I end up with LF characters left which I don't want. So the best I can do is to use StringRegExpReplace a second time to clear up the unwanted LF characters. This works as I want it. $k = "abcdef ()" For $n = 1 To 10 $t &= $k & $n & @CRLF Next $q = "abcdef" ;look for a string abcdef either at the bginning of the text or after a line feed ;with any number of horizontal white spavce chars before it, ;any number of horizintal whitespaces after then an open bracket, ;then any number of characters up to a line feed. ;Replace with "xyz" ;this works as I would expect $sText = StringRegExpReplace($t, "(?i)(\A|\n)\h*\Q" & $q & "\E\h*\(\V*", 'xyz') MsgBox(0, "result = ", $sText) ;similar but I want to replace the whole line including the CR LF at the end $sText = StringRegExpReplace($t, "(?i)(\A|\n)\h*\Q" & $q & "\E\h*\(\V*\r", '') $sText = StringRegExpReplace($sText, "(\A|\n)\n","") ;check what's left For $n = 1 To StringLen($sText) ConsoleWrite(Asc(StringMid($sText,$n,1)) & @CRLF) Next MsgBox(0, "result = ",">" & $sText & "<") Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script. Link to comment Share on other sites More sharing options...
Xenobiologist Posted May 7, 2008 Share Posted May 7, 2008 Hi martin, please post text before and after that makes it easier to get the pattern for me. Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
Siao Posted May 7, 2008 Share Posted May 7, 2008 Yes, but if I remove the \n from the pattern then I end up with LF characters left which I don't want. So the best I can do is to use StringRegExpReplace a second time to clear up the unwanted LF characters. This works as I want it. $k = "abcdef ()" For $n = 1 To 10 $t &= $k & $n & @CRLF Next $q = "abcdef" ;look for a string abcdef either at the bginning of the text or after a line feed ;with any number of horizontal white spavce chars before it, ;any number of horizintal whitespaces after then an open bracket, ;then any number of characters up to a line feed. ;Replace with "xyz" ;this works as I would expect $sText = StringRegExpReplace($t, "(?i)(\A|\n)\h*\Q" & $q & "\E\h*\(\V*", 'xyz') MsgBox(0, "result = ", $sText) ;similar but I want to replace the whole line including the CR LF at the end $sText = StringRegExpReplace($t, "(?i)(\A|\n)\h*\Q" & $q & "\E\h*\(\V*\r", '') $sText = StringRegExpReplace($sText, "(\A|\n)\n","") ;check what's left For $n = 1 To StringLen($sText) ConsoleWrite(Asc(StringMid($sText,$n,1)) & @CRLF) Next MsgBox(0, "result = ",">" & $sText & "<") If you really want to get rid of cr's and lf's at the end of the string, then you can do it in 1 pass: $sText = StringRegExpReplace($t, "(?i)(?:\A|\n)\h*\Q" & $q & "\E\h*\(.*(?:\r|\v+(?=\z))", '') "be smart, drink your wine" Link to comment Share on other sites More sharing options...
Greenhorn Posted May 7, 2008 Share Posted May 7, 2008 Hi, I don't know what exactly you want to figure out, but your 2nd example can't work if you replace all the characters with '', isn't it ? Oo Dim $t $k = "abcdef ()" For $n = 1 To 10 $t &= $k & $n & @CRLF Next $q = "abcdef" ;look for a string abcdef either at the bginning of the text or after a line feed ;with any number of horizontal white spavce chars before it, ;any number of horizintal whitespaces after then an open bracket, ;then any number of characters up to a line feed. ;Replace with "xyz" ;this works as I would expect $sText = StringRegExpReplace($t, "(?i)(\A|\n)\h*\Q" & $q & "\E\h*\(\V*", 'xyz') MsgBox(0, "result = ", $sText) ;similar but I want to replace the whole line including the CR LF at the end $sText = StringRegExpReplace($t, "(?i)(\A|\n)\h*\Q" & $q & "\E\h*\(\V*\r", 'xyz') ; xyz ??? $sText = StringRegExpReplace($sText, "(\A|\n)\n","") ;check what's left For $n = 1 To StringLen($sText) ConsoleWrite(Asc(StringMid($sText,$n,1)) & @CRLF) Next MsgBox(0, "result = ",">" & $sText & "<") For testing pattern you can use StringRegExpGUI.au3. C:\Programme\AutoIt3\Examples\Helpfile\StringRegExpGUI.au3 ... Greetz Greenhorn 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