I have a text file which is like this

29/8/2004 dsadsa dadsad dasdsa@dasda.com tfdsfce m rfdsfson2l43 Hadfsdand Rd. Frdsfsin GN 50033

5545163111 3338544355 Anytime VG337612 TN476757 Vn5676982 0.067 6700

3.5 7222233339

I want to do pattern matching in this file and do some manipulations.
For eg:

I want to find all 5 digit integers in the file and leave double space before and after it.

eg: GN 50033 5545163111 : here my output should be GN 50033 5545163111

I want to find all 2 consecutive capital characters and leave single space before and after

eg: Rd. Frdsfsin GN50033 : output should be Rd. Frdsfsin GN 50033

I want to find all 10 digit intergers and put comma before and after it

eg: 67003.5 7222233339N.A : output : 67003.5,7222233339,N.A

There are so many such patterns that are to be identified and processed. I have tried with most of the VB string functions and they are not much of an help.

Can anyone provide me with a start. If you can provide a clue or idea for how to do this type of pattern matching, I can solve all the patterns.

Please help

Dim intMsg As String
Dim StudentName As String
Open "F:\source.txt" For Input As #1
Dim filedata As String
Dim tmp As String
Dim contents As String
    While EOF(1) = 0
    Line Input #1, tmp
    contents = contents + tmp
Close #1

Using the above code i have stored all the contents of the file into a string named "contents".

So i have the data in the string format. I have already completed around 11 pattern matching using the basic string functions. There are 13 more patterns to be matched which require some special functions which i am not aware of.

Well first off, since you are wanting to match patterns that are already seperated into groups I would use the Split Function based upon a space character " " to put each group into an array. Once that is done you can loop through the array passing each group to various functions that will check to make sure what is passed is correct and format the output for you. This way it will be easy for you to not only keep track of where you are at or should be, but when you will need to call a special function to handle certain situations.

But then again, if what you have shown in the quote is a single line (or even two) it might be easier to change you reading of the file into one that leaves the vbNewLines in it so you can first split on it...

StringVariable = Input(FileLen(FileName), #FileNumber)
MyArray = Split(StringVariable, vbNewLine)

Now it looks like this file may be in a specific order and if so then you would split on the spaces...

MyWordArray = Split(MyArray(ForLoopCounter), " ")

From here you should know that in MyWordArray(0) is the first thing that needs to be verified and formatted and in the second (MyWordArray(1)) element you know that it has its own so you could do something like...

OutPutString = OutPutString & Validate1stPart(MyWordArray(0))
OutPutString = OutPutString & Validate2ndPart(MyWordArray(1))
OutPutString = OutPutString & Validate3rdPart(MyWordArray(2))
OutPutString = OutPutString & Validate4thPart(MyWordArray(3))
OutPutString = OutPutString & ValidateLastPart(MyWordArray(?)) & vbNewLine

I hope I explained that well enough for you...

Good Luck

This article has been dead for over six months. Start a new discussion instead.