Jump to content

RegEx Help


Recommended Posts

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

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 by ViciousXUSMC
Link to comment
Share on other sites

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

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 by ViciousXUSMC
Link to comment
Share on other sites

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 by mikell
Link to comment
Share on other sites

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...