I am looking for a way to negate a previously set matching pattern in order to pull out everything that is in between two characters.
I have the following code matching comments in SQL code in the "/* comment */" format. It will pick up the original code in column A and then strip the comments, placing the trimmed string in column B:
Sub FindComments()
Dim xOutArr As Variant
Dim RegEx As Object
Dim xOutRg As Range
Dim SQLString As Variant
Dim i As Integer
Dim lr As Long
lr = Worksheets("Sheet1").Cells(Rows.count, "A").End(xlUp).Row
For i = 2 To lr
SQLString = Worksheets("Sheet1").Cells(i, "A").Value
Set RegEx = CreateObject("VBScript.RegExp")
With RegEx
.Global = True
.MultiLine = True
.IgnoreCase = False
.Pattern = "(/\*(.*?)\*/)"
End With
If RegEx.test(SQLString) Then
SQLString = RegEx.replace(SQLString, "")
End If
Set RegEx = Nothing
xOutArr = VBA.Split(SQLString, ";")
Set xOutRg = Worksheets("Sheet1").Range("B" & (Worksheets("Sheet1").Cells(Rows.count, "B").End(xlUp).Row + 1))
xOutRg.Range("A1").Resize(UBound(xOutArr) + 1, 1) = Application.WorksheetFunction.Transpose(xOutArr)
Next i
End Sub
The code above will find anything written in between "/* " and " */" and then remove it, but I want to be able to also pull out anything that is in between two characters. I need to be able to match everything that does not satisfy that pattern (or some other pattern like "< comment >"). This includes line breaks, etc etc. This is specifically for VBA, and it needs to be able to search the entire string for any and all instances that that pattern appears. My goal is to put the contents in between those characters (in the pattern) into column C.
What would be the RegExp pattern for this?
Examples of SQLString would be:
1) /* Step 1 */ Select * from dual ;
2) /* Step 2 */ Select * from dual ; /* Step 3 */ Select * from Table
I am capturing the SQL code by removing the "/* Step # */" but I want to capture what is in those comments as well (in Column C). 1) and 2) are single rows. 2) has multiple queries. Each row is getting split by ";" in order to run queries one by one.