5 Replies - 576 Views - Last Post: 29 August 2016 - 01:13 AM Rate Topic: -----

#1 Whateva_  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 28-August 16

How to match multiple regex patterns in multiple files?

Posted 28 August 2016 - 06:00 AM

I want to search some regex patterns in files (*.txt) which are inside a folder whose path I'have given in a text box, and the folder contains other sub-folders with txt files in the form 12345\2031\30201\txt\120.txt and if the pattern matches even in one file, then a string is written on a log file which is created inside the folder whose path I've given in the text box and then it moves on to the next regex and so on What I've done so far is

Dim tLoc As String = TextBox1.Text
        Dim txtFilesArray = Directory.EnumerateFiles(tLoc, "*.txt", SearchOption.AllDirectories).Where(Function(f) f Like "*\#*\#*\#*\txt\#*.txt")
        Dim fileLoc As String = tLoc & "\Checklist.log"
        Dim fs As FileStream = Nothing
        If (Not File.Exists(fileLoc)) Then
            fs = File.Create(fileLoc)
            Using fs

            End Using
        End If
        For Each tFile In txtFilesArray
            Dim input As String = File.ReadAllText(tFile)
            Dim pattern1 As New Regex("(?<!>)(figure|fig\.|figs\.|figures) (\d+)")
            Dim pattern2 As New Regex("(?<!>)(table|tab\.|tabs\.|tables) (\d+)")
            If pattern1.IsMatch(input) Then
                FileOpen(1, fileLoc, OpenMode.Append)
                PrintLine(1, "Check figure link")
                FileClose()
            End If
            If pattern2.IsMatch(input) Then
                FileOpen(1, fileLoc, OpenMode.Append)
                PrintLine(1, "Check table link")
                FileClose()
            End If

        Next

But the problems are: 1) Even if pattern1 matches in multiple files, I want it to write the string Check figure link only once in the log file and not in every time it finds a match in different files and same for pattern2....patternN, furthermore, I want the program to move on to the next regex pattern match the moment the pattern1 matches in one file (no need to look for the same pattern in other files) 2)I have around a hundred of regex patterns that I want to use in this program, can anyone tell me how do I shorten the coding?

Is This A Good Question/Topic? 0
  • +

Replies To: How to match multiple regex patterns in multiple files?

#2 RamonRobben  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 87
  • View blog
  • Posts: 559
  • Joined: 19-May 14

Re: How to match multiple regex patterns in multiple files?

Posted 28 August 2016 - 06:50 AM

just do something like 2 variables.

Dim checkedPatternOne = false;


if checkPatternOne is false and it is a match then do some stuff.
If checkPatternOne is true then don't check it anymore.

as in

if pattern1.IsMatch(input) and checkPatternOne == false then
checkPatternOne = true;
end if


You can do the same for pattern2.
Not sure if this is what you want but..

This post has been edited by RamonRobben: 28 August 2016 - 06:50 AM

Was This Post Helpful? 1
  • +
  • -

#3 Whateva_  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 28-August 16

Re: How to match multiple regex patterns in multiple files?

Posted 28 August 2016 - 07:35 AM

It shows an error
Error BC30201 Expression expected.
The rewritten code as per your suggestion @RamonRobben

 Dim checkedpattern1 = False
            If pattern1.IsMatch(input) And checkedpattern1 == False Then
                checkedpattern1 = True
                FileOpen(1, fileLoc, OpenMode.Append)
                PrintLine(1, "Check figure link")
                FileClose(1)

            End If

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,474
  • Joined: 12-December 12

Re: How to match multiple regex patterns in multiple files?

Posted 28 August 2016 - 12:45 PM

I don't understand what you are hoping to achieve. It looks like you want to search for 100+ regex across (possibly) hundreds of files, ending up with a list that tells you "yes, some of these expressions occur in some of these files; I don't know which files, or how many times the expressions occur"?
Was This Post Helpful? 0
  • +
  • -

#5 Whateva_  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 66
  • Joined: 28-August 16

Re: How to match multiple regex patterns in multiple files?

Posted 28 August 2016 - 07:44 PM

@andrewsw I'm hoping to achieve a list of patterns (out of say 100 patterns) which match at least in one file and if I could get the information's like how many times a pattern matches in what files that would be great, but I don't know how to achieve that as I'm a beginner in coding and that is why I made the program as simple as possible. But if you could help me achieve those, I'll be grateful.
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is offline

  • blow up my boots
  • member icon

Reputation: 6541
  • View blog
  • Posts: 26,474
  • Joined: 12-December 12

Re: How to match multiple regex patterns in multiple files?

Posted 29 August 2016 - 01:13 AM

You can use Regex.Matches(text, pattern).Count to count the number of matches for an expression.

To make code shorter with 100 regex put them in a collection of some kind and iterate the collection.

As you are a beginner I suggest you start with simpler examples and projects. Otherwise, if you want to continue with this, start with a much simpler version of the task; get it working then start to increase the complexity.

A broader description would also help. What are these files and what information are you looking for? But, as I say, it is your task, so, as a beginner, you need to start simpler or come back to it later when you have sufficient knowledge and confidence to tackle it. Also, when you come back to it, write a clear outline and pseudocode for your task, otherwise you'll have a frustrating time with no clear goal(s).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1