soft4pedia Posted March 11, 2016 Share Posted March 11, 2016 Hello, I'm having several type of text files but my text files are having unnecessary space because of enters (end of line or line breaks) but I want my text in continuous stream. There is no basically proper order of enter-spaces (end of line breaks) sometime there are two line breaks, sometime 3 to 8 lines breaks but I only want 1 line break at the end of end...Please help me which pattern will be suitable for me to use "StringRegExReplace" command to remove unnecessary breaks in my text files.. $File1 = @ScriptDir & "\text01.txt" $txt = FileRead($File1) ; Please recommend me which pattern will be fine to remove unnecesary end of line Enter-spaces $txt = StringRegExpReplace($txt, 'Not sure about pattern', "") FileWrite (@scriptdir & "\text02", $txt) Link to comment Share on other sites More sharing options...
InunoTaishou Posted March 11, 2016 Share Posted March 11, 2016 (edited) Global $sExample = "This" & @CRLF & @CRLF & "is an example." & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & "This is another line" & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & "with some extra" & @CRLF & @CRLF & @CRLF & @CRLF & "line breaks." & @CRLF ConsoleWrite("=================Before=================" & @LF & $sExample & @LF & "=================Before=================" & @LF) $sExample = StringRegExpReplace($sExample, "(?m)(\r\n){2,}", @CRLF) ConsoleWrite("=================After=================" & @LF & $sExample & @LF & "=================After=================" & @LF) Quote (?m) : Multiline (\r\n) : Looking for \r\n together {2,} : 2 or more \r\n together. Edited March 11, 2016 by InunoTaishou soft4pedia 1 Link to comment Share on other sites More sharing options...
soft4pedia Posted March 12, 2016 Author Share Posted March 12, 2016 Thanks mate for suggesting me codes but these codes are relatively hard for me to understand; I tried to solve the problem on my own and design the following code; $File1 = @ScriptDir & "\kat01.txt" $txt = FileRead($File1) $txt = StringReplace($txt, @crlf, "<br>") In this way got repalced all the ending lines with <br> code and then again want to try this code but it's unable to work; $File1 = @ScriptDir & "\kat01.txt" $txt = FileRead($File1) $txt = StringRegExpReplace($txt, "<br><br>", "") $txt = StringReplace($txt, "<br><br>", "") I found that when i just removed <> from my replacement code and use br only to replace it just remove br from my data. In the same way, when i tried to use code <br> or <br><br> to replace with nothing it'll never replace anything inside my text file....Please help me what wrong with my codes? is the StringReplace or StringRegExpReplace never support replacement of brackets or text inside the brackets? Link to comment Share on other sites More sharing options...
InunoTaishou Posted March 12, 2016 Share Posted March 12, 2016 Alright, how about this. We'll use a loop that will continue to execute while there are still multiple @CRLF@CRLFs in the string. ; Our example string Local $sExample = "This" & @CRLF & @CRLF & "is an example." & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & "This is another line" & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & "with some extra" & @CRLF & @CRLF & @CRLF & @CRLF & "line breaks." & @CRLF ; Create your variable that will be used for the while loop Local $iCrlf2 = StringInStr($sExample, @CRLF & @CRLF) ; $iCrlf2 is the index in the string where @CRLF&@CRLF is. If it's in the string then it will be > 0, if it's not then $iCrlf2 will be 0 ; and the loop will exit While ($iCrlf2) ; Replace the double @CRLF with a single @CRLF ; I.e., there is @CRLF & @CRLF & @CRLF in one spot, this will replace the first instance of @CRLF & @CRLF with only 1 @CRLF, ; thus making 3x@CRLF turn into 2x@CRLF. When the loop executes again, that 2x@CRLF now becomes 1x@CRLF $sExample = StringReplace($sExample, @CRLF & @CRLF, @CRLF) ; Update $iCrlf2 with the new index in the string $iCrlf2 = StringInStr($sExample, @CRLF & @CRLF) WEnd ConsoleWrite($sExample & @LF) Hopefully my comments make sense. The loop will execute when it finds 2x@CRLF in a row. It will then replace the 2x@CRLF with 1x@CRLF. So all multiple instances of @CRLF next to each other will be subtracted by one on each pass through. Take, for instance, the first long @CRLF chain. Quote @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF 7 line feeds. On the first pass through it will replace the first 2x@CRLF with one, so it will subtract one from the chain making it 6. Continueing to do this while there are still 2x@CRLF in the string. soft4pedia 1 Link to comment Share on other sites More sharing options...
iamtheky Posted March 12, 2016 Share Posted March 12, 2016 Local $sExample = "This" & @CRLF & @CRLF & "is an example." & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & "This is another line" & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & "with some extra" & @CRLF & @CRLF & @CRLF & @CRLF & "line breaks." & @CRLF $sFrmt = StringRegExpReplace(stringstripCR($sExample) , "\n+" , @LF ) ;strip carraige returns, any time there is more than one line feed, replace that group with a single @LF msgbox(0, '' , $sFrmt) soft4pedia 1 ,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-. |(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/ (_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_) | | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) ( | | | | |)| | \ / | | | | | |)| | `--. | |) \ | | `-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_| '-' '-' (__) (__) (_) (__) Link to comment Share on other sites More sharing options...
soft4pedia Posted March 12, 2016 Author Share Posted March 12, 2016 Hello iamtheky, Thanks you so much for StringStripCR command, other people also did very good job but this command is easy for me to understand. Link to comment Share on other sites More sharing options...
jguinch Posted March 12, 2016 Share Posted March 12, 2016 Also, an easy way with StringRegExpReplace : Local $content = "This" & @CRLF & @CRLF & "is an example." & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & "This is another line" & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & @CRLF & "with some extra" & @CRLF & @CRLF & @CRLF & @CRLF & "line breaks." & @CRLF $newContent = StringRegExpReplace($content, "\R+", @CRLF) ConsoleWrite($newContent) Spoiler Network configuration UDF, _DirGetSizeByExtension, _UninstallList Firefox ConfigurationArray multi-dimensions, Printer Management UDF 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