How to write output file after all regexp match? - regex

I have text file containing line starts with mmrk.
I want to extract all lines and write output.
I am trying following code. (I know this is not correct method, Just to show what I want.)
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, MyFile, FileName, Text
dim oFile, strPath
Set fso = CreateObject("Scripting.FileSystemObject")
FileName = "C:\Users\user\Desktop\2.rtf"
strPath = "C:\Users\user\Desktop\1.txt"
Set MyFile = fso.OpenTextFile(FileName, ForReading)
Do While MyFile.AtEndOfStream <> True
Text = MyFile.ReadAll
Loop
Set regEx_ = new regExp
With regEx_
.Global = True
.MultiLine = True
.IgnoreCase = True
'Do some regex find and replace(works perfectly)
.Pattern = "mmrk.*"
If regEx_.Test(Text) Then
Set oFile = fso.OpenTextFile(strPath, 2, True, -1)
oFile.Write Text
oFile.Close
End If
End With
MyFile.Close
Edit
I need to get lines from string.
I am currently first saving temp file from string and use Nefariis's answer to do the rest. Is there any direct method.

I think there are easier ways of doing this that do not involve a regex.
In VBScript:
FileName = "C:\Users\user\Desktop\2.rtf"
strPath = "C:\Users\user\Desktop\1.txt"
Set inFile = CreateObject("Scripting.FileSystemObject").OpenTextFile(FileName)
Set outFile = CreateObject("Scripting.FileSystemObject").CreateTextFile(strPath,True)
Do Until inFile.AtEndOfStream
Dim line : line = inFile.Readline
If inStr(line, "mmrk.") = 1 then outFile.writeLine(line)
Loop
inFile.Close
outFile.Close
In VB.Net
Dim inFile as String() = File.ReadAllLines("inFileName")
Dim sw As StreamWriter = New StreamWriter("OutFileName", True)
For Each line As String In inFile
If line.StartsWith("mmrk.") Then sw.WriteLine(line)
Next
sw.Close()
This reads the file in, then goes through it looking for lines the start with "mmrk.", and saves the line into a seperate text file that you specifiy.

Related

How to replace double quotes to a space in VBScript?

I need open the CSV file, replace the double quotes with space and save the new file with txt extension.
I have tried this VBScript code but the first file Output_D1.txt is empty, the Output_D2.txt file contains rows of the Output_D1.csv, the Output_D3.txt file contains rows of the Output_D2.csv... etc.
How to do resolve this ?
nArr = Array("D1", "D2", "D3", "D4", "D5", "D6")
Set reP = new RegExp
reP.Pattern = "\"""
For I = 0 To UBound(nArr)
InFilename = "Output_" & nArr(I) & ".csv"
Set FILE1 = CreateObject("scripting.FileSystemObject")
Set infile = FILE1.OpenTextFile(InFileName, 1, False)
strg = reP.Replace(strg, " ")
InFilenameNew = "Output_" & nArr(I) & ".txt"
Set Outfile = File1.CreateTextFile(inFileNameNew, 1, False)
Outfile.Write(strg)
strg = infile.ReadAll
infile.Close
Next
Try this:
You need to set the RegExp global option to replace all occurences of quotes
You need the full path to the CSV files (see rootFolder for a hard-coded example)
ReadAll is used in the wrong place (it needed to be before the RegExp replace!)
Option Explicit
dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
dim rootFolder : rootFolder = "C:\Temp\"
dim nArr : nArr = Array("D1", "D2", "D3", "D4", "D5", "D6")
dim i, inFilename, inFilenameNew, infile, outfile, filecontent
Const ForReading = 1, ForWriting = 2
dim reP : Set reP = new RegExp
reP.Global = true
reP.Pattern = "\"""
For i = 0 To UBound(nArr)
inFilename = rootFolder & "Output_" & nArr(i) & ".csv"
inFilenameNew = rootFolder & "Output_" & nArr(i) & ".txt"
if (fso.FileExists(inFilename)) Then
Set infile = fso.OpenTextFile(InFileName, ForReading)
filecontent = infile.ReadAll
filecontent = reP.Replace(filecontent, " ")
infile.Close
Set infile = Nothing
Set outfile = fso.CreateTextFile(inFileNameNew, True)
outfile.Write(filecontent)
outfile.Close
Set outfile = Nothing
End If
Next
Set reP = Nothing
Set fso = Nothing

Regex Adding Unnecessary Spaces During Replace

I'm currently having difficulty with a VBScript I'm writing that contains several read and replaces from a text file. The expression I'm using finds the expression and replaces it, but adds three tab spaces afterwords, making the original line below it mess up the formatting. Here's a picture of what I'm talking about:
Here's a pastebin of the before and after, rather than an image:
https://pastebin.com/Uw3H59QK
Here's my RegExp code:
Set fso = CreateObject("Scripting.FileSystemObject")
Dim strPath
strPath = SelectFolder( "" )
If strPath = vbNull Then
WScript.Echo "Script Cancelled - No files have been modified" 'if the user cancels the open folder dialog
Else
WScript.Echo "Selected Folder: """ & strPath & """" 'prompt that tells you the folder you selected
End If
Function SelectFolder( myStartFolder )
Dim objFolder, objItem, objShell
Dim objFolderItems
On Error Resume Next
SelectFolder = vbNull
Set objShell = CreateObject( "Shell.Application" )
Set objFolder = objShell.BrowseForFolder( 0, "Please select the .dat file location folder", 0, myStartFolder)
set objFolderItems = objFolder.Items
If IsObject( objFolder ) Then SelectFolder = objFolder.Self.Path
Set objFolder = Nothing
Set objShell = Nothing
On Error Goto 0
End Function
Set re = New RegExp 'Replacing Position Lines
re.Pattern = "Pos = \((.*)\)"
re.Global = True
re.IgnoreCase = True
For Each f in fso.GetFolder(strPath).Files
If LCase(fso.GetExtensionName(f.Name)) = "txt" Then
text = f.OpenAsTextStream.ReadAll 'reading the text file
f.OpenAsTextStream(2).Write re.Replace(text, """Position"" : mathutils.Vector(($1)),")
count = count + 1
End If
Next
Set reAngles = New RegExp 'Replacing Angles
reAngles.Pattern = "Angles = \((.*)\)"
reAngles.Global = True
reAngles.IgnoreCase = True
For Each f in fso.GetFolder(strPath).files
If LCase(fso.GetExtensionName(f.Name)) = "txt" Then
text = f.OpenAsTextStream.ReadAll
f.OpenAsTextStream(2).Write reAngles.Replace(text, """Angles"" : mathutils.Vector(($1)),")
End If
Next
Set reNames = New RegExp 'Replacing Names
reNames.Pattern = "Name = (.*)"
reNames.Global = True
'reNames.Multiline = True
reNames.IgnoreCase = True
For Each f in fso.GetFolder(strPath).files
If LCase(fso.GetExtensionName(f.Name)) = "txt" Then
text = f.OpenAsTextStream.ReadAll
f.OpenAsTextStream(2).Write reNames.Replace(text, """Name"" : ""$1"",")
End If
Next
My best guess is that the wildcard is grabbing more info than needed...but I'm unsure how to fix that. I used a lot of these expressions in Notepad++ so I was hoping to translate them to a VBS easily!

VBA Regular expression with string split

Can anyone please help me out with Vba macro.
I'm using the below mentioned code. The task is to read a notepad file which contains contents and extract a certain string which looks like "Z012345" and paste them in excel row wise such cell A1 will Z067859 and A2 would be Z002674 etc.,
A sample of how the contents in the notepad file looks like
Contents:
RAF0A123 Full data len= 134
ABATWER01 Recent change by VT0123123 on 11/12/17-11:50
INCLUDE(STELLER Z067859 Z002674 Z004671 Z003450 Z005433 Z023123 Z034564 Z034554 Z043212 Z010456 Z014567
Z027716 Z028778 Z029439 Z029876 Z035766 Z036460 Z038544 Z046456 Z047680 Z052907 Z053145 Z074674 Z094887
VBA code:
Sub Demo()
Dim myFile As String, text As String, textline As String
Dim regex As Object, str As String
Set regex = CreateObject("VBScript.RegExp")
myFile = "C:\Users\sample.txt"
Open myFile For Input As #1
With regex
.Pattern = "Z0[0-9]+"
.Global = Trueq
End With
Set matches = regex.Execute(Input)
For Each Match In matches
Range("A1:A4000").Value = Match.Value
Next Match
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
End Sub
Expected output:
Excel output column A should contain the below:
Z067859
Z002674
Z004671
Z003450
Z005433
Z023123
Z034564
Z034554
Z043212
Z010456
Z014567
Z027716
Z028778
Z029439
Z029876
Z035766
Z036460
Z038544
Z046456
Z047680
Z052907
Z053145
Z074674
Z094887
Could anyone help me out to write a macro to perform the task?
Rather than reading one line at a time, I would rather read the entire file into a string and then find the string and paste it. Sample code
Dim myFile As String, regex As Object, str As String, ctr As Long
myFile = "C:\Users\sample.txt"
With CreateObject("Scripting.FileSystemObject")
str = .OpenTextFile(myFile, 1).ReadAll
End With
Set regex = CreateObject("VBScript.RegExp")
With regex
.Pattern = "Z0[0-9]+"
.Global = True
End With
Set matches = regex.Execute(str)
ctr = 1
For Each Match In matches
Sheet1.Range("A" & ctr).Value2 = Match
ctr = ctr + 1
Next Match
I actually think your code is 85% there. I see a couple of things wrong.
1) You need to read the file before you try to output to Excel. In your code it seems you read the file after any activity in Excel
2) You are putting the same value in every single cell from A1 to A1000, overwriting them each time. I believe you want to loop down and put each value in a cell.
3) You're passing a variable that doesn't even exist to your regex
A couple of changes, and this might do it:
Sub Demo()
Dim myFile As String, text As String, textline As String
Dim regex As Object, str As String
Set regex = CreateObject("VBScript.RegExp")
myFile = "C:\Users\sample.txt"
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
Close #1
With regex
.Pattern = "Z0[0-9]+"
.Global = True
End With
Set matches = regex.Execute(text)
Dim row As Long
row = 1
For Each Match In matches
Cells(row, 1).Value2 = Match
row = row + 1
Next Match
End Sub
Please try the below and let me know it meets your requirement
Sub Demo()
Dim myFile As String, text As String, textline As String
Dim str As String
Dim LineArray() As String
Dim DataArray() As String
Dim TempArray() As String
Dim rw As Long, col As Long
Dim FileContent As String
Set regex = CreateObject("vbscript.regexp")
Dim allMatches As Object
Delimiter = " "
myFile = "Path\sample.txt"
With regex
.Pattern = "Z0[0-9]+"
.Global = True
End With
Open myFile For Input As #1
Do Until EOF(1)
Line Input #1, textline
text = text & textline
Loop
LineArray() = Split(text, vbCrLf)
i = 1
For x = LBound(LineArray) To UBound(LineArray)
If Len(Trim(LineArray(x))) <> 0 Then
TempArray = Split(LineArray(x), Delimiter)
col = UBound(TempArray)
ReDim Preserve DataArray(col, rw)
For y = LBound(TempArray) To UBound(TempArray)
Set allMatches = regex.Execute(TempArray(y))
Range("A" & i).Value = allMatches.Item(0)
i = i + 1
Next y
End If
rw = rw + 1
Next x
Close #1
End Sub
Thanks

Clean blanks/whitespace to vbNull with RegEx

I am looking to clean up a .csv file for a database import. I am using the following vbs function and would like to incorporate '' to vbNull. I find it hard to understand RegEx. Can this even be done?
Function removeEmbeddedCommasInCSVTextField (strtoclean)
Dim objRegExp, outputStr
Set objRegExp = New Regexp
objRegExp.IgnoreCase = True
objRegExp.Global = True
objRegExp.Pattern = """[^""]*,[^""]*"""
Set objMatch = objRegExp.Execute( strtoclean )
corrected_row = strtoclean
For Each myMatch in objMatch
matched_value = myMatch.Value ' retrieves text column with embedded commas
cleaned_value = replace(matched_value, ",","") ' removes embeddes commans from column
corrected_row = replace(corrected_row, matched_value, cleaned_value) 'take row and replaced bad value with good value (no commas)
Next
removeEmbeddedCommasInCSVTextField = corrected_row
End Function
MAIN:
Set MyFile = fso.CreateTextFile(strShareDirectory & "fixed.txt", True)
Set f = fso.OpenTextFile(strShareDirectory & filename)
Do Until f.AtEndOfStream
before_clean = f.ReadLine
after_clean = removeEmbeddedCommasInCSVTextField(before_clean)
MyFile.WriteLine(after_clean)
'WScript.Echo after_clean
Loop
f.Close
MyFile.Close

Regex pattern not working in VB Script

I have this VBS code:
Option Explicit
Dim reMethod, reInterface
Dim vaction
Dim fileService
Dim mService
Dim lineService
Dim objFSO
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set fileService = objFSO.OpenTextFile("GIISAssuredController.java" , ForReading)
Set reMethod = new regexp
reMethod.Pattern = """\w+?""\.equals\(ACTION\)[\s\S]*?\{[\s\S]*?\.([^(]*)\("
reMethod.IgnoreCase = True
reMethod.Global = True
Do Until fileService.AtEndOfStream
lineService = fileService.ReadLine
For Each mService In reMethod.Execute(lineService)
vaction = mService.Submatches(0)
Set reInterface = new regexp
Wscript.Echo vaction
Next
Loop
And 'GIISAssuredController.java':
} else if ("hello".equals(ACTION)) {
Integer assuredNo = giisAssuredService.saveAssured(assured);
The regex pattern is not working.
I am expecting the output to be is:
saveAssured
But instead, it's not echoing anything. I tried the regex pattern here > https://regex101.com/r/kH3aZ4/1, and it's getting the 'saveAssured' string.
This question is related to: Multiline REGEX using VB Script
If the expression needs to match a text that spawns over multiple lines, but you read the file line by line and test line by line, there will never be a match.
Option Explicit
Const ForReading = 1
Dim code
code = CreateObject("Scripting.FileSystemObject" _
).OpenTextFile("GIISAssuredController.java" , ForReading _
).ReadAll()
Dim mService
With new RegExp
.Pattern = """\w+?""\.equals\(ACTION\)[\s\S]*?\{[\s\S]*?\.([^(]*)\("
.IgnoreCase = True
.Global = True
For Each mService in .Execute(code)
WScript.Echo mService.Submatches(0)
Next
End With