Solved

# Regular Expression Help

Posted on 2006-03-28
Medium Priority
223 Views
I need help adding the regular expression to the Regex line to find the value “Not in Database” and error checking.  Columns 5 and 8 are where the information may be located.  Also the file is VbTab delimited.

Here is what I have so far:

Imports System.IO
Imports System.Text.RegularExpressions

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Try
Dim re As New Regex( )

Dim input As String
Dim output As String

sr.Close()

If input <> output Then
Dim sw As New StreamWriter("C:\resource\NIDB.txt")
sw.Write(output)
sw.Close()
End If

Thanks for your time and expertise,
Gr8life
0
Question by:gr8life
• 4
• 3

LVL 5

Assisted Solution

razorback041 earned 200 total points
ID: 16315387
I know this isnt regex, but with tabdelim, this seems easier. I am also assuming there is more than one line of values to evaluate.  If you are needing to use RegEx, I am sure someone will chime in soon.

dim newLine as string
do until sr.peek = -1
Dim DataFieldData() As String =  newLine.Split(ControlChars.Tab)
Dim input As String = DataFieldData(4)
Dim output As String = DataFieldData(7)

'code to evaluate/write goes here
loop
sr.Close()

0

LVL 64

Expert Comment

ID: 16315759
Hi gr8life;
Not sure what you want. In your statement 'find the value "Not in Database” and error checking'. Does this mean that if column 5 has a value it will contain the phrase "Not in Database” and if column 5 has this phrase in it does that mean that you want the value in column then?

Can this happen on multiple lines in the file?

You define two string variables:

Dim input As String
Dim output As String

you assign a value to input and later on in the code you compare it to output but you never assign a value to output?

Let me know I would be glad to help with a Regex pattern.

Fernando
0

Author Comment

ID: 16316916
After reading the reply from razorback041, I was wondering if his approach is more efficient to process large data sets up to 2 gig in size?

The "Not in Database" value can occur on multiple lines, twice on one line, or not on any line.

Sample data:

cat      cat      cat      Not in Database      cat      cat      Not in Database      cat
dog      dog      dog      Not in Database      dog      dog      dog                  dog
rat      rat      rat      rat                  rat      rat      rat                  rat

I added extra tabs to illustrate the columns better.  Also the input file has more columns than I have posted here.

The error check I was referring to is if the document doesn’t have any occurrences of “Not in Database” then message box, No Matches.

Thank you for taking the time to read this post,
Gr8life
0

LVL 64

Expert Comment

ID: 16317423
Hi gr8life;

The following code will read the input data into the variable input. search the input for "Not in Database" with the Regex object. Then writes all the lines that have the phrase in the line to a file.

Imports System.Text.RegularExpressions
Imports System.IO

Dim re As New Regex("^.*?Not\sin\sDatabase.*?$", RegexOptions.Multiline) Dim sr As New StreamReader("C:\resource\input.txt") Dim input As String = sr.ReadToEnd() sr.Close() Dim mc As MatchCollection Dim sw As New StreamWriter("C:\resource\input_NotInDB.txt") mc = re.Matches(input) For Each m As Match In mc ' If you have any matches in the input data the whole line that it ' appears in will be one of the matches from the match collection. sw.Write(m.Value) Next sw.Close() Fernando 0 Author Comment ID: 16317574 FernandoSoto, I tried the code you posted and I works great however it writes all the data to one line. Is there a way to write the data into several lines like this: cat cat cat Not in Database cat cat Not in Database cat dog dog dog Not in Database dog dog dog dog instead of: cat cat cat Not in Database cat cat Not in Database cat dog dog dog Not in Database dog dog dog dog Thanks for your time, Gr8life 0 LVL 64 Accepted Solution Fernando Soto earned 1800 total points ID: 16317688 Hi gr8life; When you created the Regex object did you write it as I did: Dim re As New Regex("^.*?Not\sin\sDatabase.*?$", RegexOptions.Multiline)

Because the ^ in the Regex pattern marks the begining of a line and the \$ marks the end of a line as long as the RegexOptions.Multiline is in the statement as above.

The other thing is that the file is one continus line with no CrLf characters at the end of each line.

But if you did the above and it is still comming out as one line then you can try this. Use the WriteLine in place of the Write.

sw.WriteLine(m.Value)

Fernando
0

LVL 64

Expert Comment

ID: 16317719
In fact the way the pattern is set up if you do not have the RegexOptions.Multiline in the creation of the Regex object it will take the whole input as only one line.
0

Author Comment

ID: 16317749
It works great!

Thank you very much,
Gr8life
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
###### Suggested Courses
Course of the Month17 days, 9 hours left to enroll