xcaliber13 Posted May 20, 2016 Share Posted May 20, 2016 Good Day to All, I have been reading and trying to understand the regex. Can I get a little help please. I have a text file the has number lines. Example 1 some text here 2 more text here 3 and more text here and repeats to end of file. What I can't seem to figure out is the regex that would take any line beginning with 1 or 2 and extract those lines to a single line. Thank you Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted May 20, 2016 Share Posted May 20, 2016 (edited) I am not a regex expert so somebody (MIkell lol) can do better but I have the concept. In RegEx $ marks the beginning of a line so use that in conjunction with what your looking for to get your matches. $aResults = StringRegExp("test", "^[12].*", $STR_REGEXPARRAYGLOBALMATCH) Use StringRegExp to get all results in an array, and then loop through that array to combine the results the way you want. Here is how the RegEx should work: https://regex101.com/r/nX8vS5/1 Edited May 20, 2016 by ViciousXUSMC Link to comment Share on other sites More sharing options...
xcaliber13 Posted May 20, 2016 Author Share Posted May 20, 2016 Here is what I have so far. But it is only pulling the first line that begins with the number 1 #include <file.au3> #include <Array.au3> #include <String.au3> #include <StringConstants.au3> $vfile = "C:\Temp\test.txt" $hFile = FileOpen($vFile) $sFile = FileRead($hFile) Local $aResults[0] $aResults = StringRegExp($sFile, "^[12].*", 3) _ArrayDisplay($aResults) The test.txt file looks like this: 1 This is a test 2 This is a test 3 This is a test 4 This is a test 1 This is another test 2 This is another test 3 This is another test 4 This is another test 1 This is a test more 2 This is a test more 3 This is a test more 4 This is a test more 1 This is another test more 2 This is another test more 3 This is another test more 4 This is another test more Any help? Please. The final results of what I am trying to do is to extract all lines that begin with a 1 or 2 and write to a file so that the file would look like this: 1This is a test 2 This is a test 1This is another test 2 This is another test until end of file. Thank you Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted May 20, 2016 Share Posted May 20, 2016 (edited) Add t he (?m) flag for multiline. $aResults = StringRegExp($sFile, "(?m)^[12].*", 3) $aResults = StringRegExp($sFile, "(?m)^[12].*", 3) A working example #include <file.au3> #include <Array.au3> #include <String.au3> #include <StringConstants.au3> $sFile = FileRead(@ScriptDir & "\test.txt") Local $aResults[0] $aResults = StringRegExp($sFile, "(?m)^[12].*", 3) Local $aFinalResults[UBound($aResults)] _ArrayDisplay($aResults) For $i = 0 to UBound($aResults) -1 $aFinalResults[$i] = $aResults[$i] & " " & $aResults[$i+1] $i = $i+1 Next _ArrayDisplay($aFinalResults) For $i = UBound($aFinalResults) -1 To 0 Step -1 If $aFinalResults[$i] = "" Then _ArrayDelete($aFinalResults, $i) EndIf Next _ArrayDisplay($aFinalResults) _FileWriteFromArray(@ScriptDir & "\Test2.txt", $aFinalResults) Edited May 20, 2016 by ViciousXUSMC Link to comment Share on other sites More sharing options...
xcaliber13 Posted May 20, 2016 Author Share Posted May 20, 2016 Now that worked so dam good it hurt. Thank you ViciousXUSMC you are the best. And just another reason why I am moving away for VBS Scripting to AutoIt. Just unbelievable help. Link to comment Share on other sites More sharing options...
mikell Posted May 20, 2016 Share Posted May 20, 2016 (edited) For the fun #include <File.au3> _FileWriteFromArray(@ScriptDir & "\Test2.txt", _ StringRegExp(StringReplace(FileRead(@ScriptDir & "\test.txt"), @crlf, @tab), "(?<=^|\t)(1[^\t]*\t2[^\t]*)", 3)) And if needed, each result line can be splitted using tab Edit More fun Msgbox(0,"", StringRegExpReplace(StringRegExpReplace(FileRead(@ScriptDir & "\test.txt"), '(?m)^[^12].*\R?', ""), '\R(?=2)', " ")) Edited May 20, 2016 by mikell Link to comment Share on other sites More sharing options...
Malkey Posted May 22, 2016 Share Posted May 22, 2016 Another example. Local $sTestExampleModified = "TestExampleModified.txt" ; Name of the new, modified "Test.txt" file. If FileExists($sTestExampleModified) Then FileDelete($sTestExampleModified) FileWrite($sTestExampleModified, StringRegExpReplace(FileRead("Test.txt"), "(?m)(^1.+)\R(^2.+\R?)(^[^12].+\R?)*", "\1 \2")) ProcessWaitClose(ShellExecute($sTestExampleModified)) ; Display modified txt file (Close this window to continue script). FileDelete($sTestExampleModified) ; Clean up created file. Link to comment Share on other sites More sharing options...
jguinch Posted May 22, 2016 Share Posted May 22, 2016 One shot Msgbox(0, "", StringRegExpReplace(FileRead(@ScriptDir & "\test.txt"), "(?:^|\R)(?|1(\h)\N+\K\R|\d\N+())", "\1")) 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