Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 397
  • Last Modified:

Vexing regex in VB

I've been wrestling with this for days. I'm trying to find a string between two markers in a text file. The markers are "Begin InputTables" and "Begin OutputColumns". The string I need to find is the text between double quotes, i.e. "PCODE" and "CLIENTSX" in:

Begin InputTables
    Name ="PCODE"
    Name ="CLIENTSX"
End
Begin OutputColumns

I can get the regex to find the string if I move the string up to a point immediately after the look-behind...

"Begin InputTables PCODE"

... but of course that does me no good since what I have are hundreds of files where the string is in a line after "Begin InputTables".

I'm searching for the string in For... Each iterations, so I only need to find one at a time.

Here's the code:

                For Each tableInQueryToFind In txtInputTablesInDir
                    If textIn.Peek <> -1 Then
                        Dim tableNm As String = tableInQueryToFind.Name
                        Dim findText As String = tableNm
                        Dim strLength As Integer = Len(findText)
                        findText = findText.Substring(0, strLength - 4)
                        Dim pattern As String = "(?<=^Begin InputTables$)" & findText & "\b" ' & "+(?<=End)??$"
                        Dim options As RegexOptions = RegexOptions.IgnoreCase Or RegexOptions.Multiline
                        Dim m As Match = Regex.Match(line, pattern, options)
                        If Not IsNothing(line) Then
                            If m.Success Then
                                My.Computer.FileSystem.WriteAllText(tableInQueriesOutputFullPath, "<query>" & queriesDirFileNm.Name & "</query>" & "<tableinquery>" & findText & "</tableinquery>" & vbNewLine, True)
                                Exit For
                            End If
                        Else
                            Exit For
                        End If
                    End If
                Next

Open in new window

0
aanuncio
Asked:
aanuncio
  • 3
  • 2
3 Solutions
 
Dan CraciunIT ConsultantCommented:
This:
@"InputTables.*?""(\w+)"".*?""(\w+)"".*?OutputColumns"

Open in new window

will store in $1 and $2 your required text.

HTH,
Dan
0
 
RannyMeierCommented:
Have you considered using MatchCollection?
We can get all of the InputTable names into a Regex MatchCollection.  Then we can use Linq.Any() method to test.

Regex rx = new Regex(@"(?<Records>\s*Name ="(?<Name>[A-Z]+)"\s+)", RegexOptions.IgnoreCase);

MatchCollection matches = rx.Matches(text);
0
 
aanuncioAuthor Commented:
I see that I need to simplify the question. What I need to know is why my look-behind isn't finding anything beyond the first line.

I've tried every combination of line endings to get past the carriage return, but nothing seems to work.

I'll also try the MatchCollection approach, but now that I've started down this road, I'd really like to know why the regex doesn't seem to work following normal conventions.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
aanuncioAuthor Commented:
This...

(?<=^Begin InputTables\r\n.*)PCODE(?=.*\r\n.*\r\nEnd\r\n^Begin OutputColumns)
... should work to find "PCODE". So why doesn't it?
0
 
RannyMeierCommented:
I believe that
(?<=^Begin InputTables\r\n.*)PCODE(?=.*\r\n.*\r\nEnd\r\n^Begin OutputColumns)
does find the word PCODE.

Does the program script above result in this same search pattern string?  I did not see that in the original question post.
0
 
aanuncioAuthor Commented:
I found the problem, and it was me.

It turns out that no multiline regex (including the correctly syntaxed one verfified by RannyMeier) could possibly work because the input string was a single line. Doh!

Thank you all for putting time into this.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now