HI,
I have a text file which is like this

29/8/2004 dsadsa dadsad 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
    Wend
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.

Dani AI

Generated

For : instead of ad-hoc string fiddling, use regular expressions to find exact-length tokens and apply replacements in a controlled order. Build on ’s idea of tokenizing, but use a regex tokenizer or a small sequence of ordered replacements so longer matches (10 digits) are handled before shorter ones (5 digits) and letter/digit boundaries are fixed first.

A quick, order-sensitive approach in VB.NET (reads from your existing contents string):

Imports System.Text.RegularExpressions

Dim text = contents

' 1) wrap exact 10-digit numbers with commas
text = Regex.Replace(text, "(?<!\d)(\d{10})(?!\d)", ",$1,")

' 2) ensure two-letter caps immediately before digits get a space (GN50033 -> GN 50033)
text = Regex.Replace(text, "([A-Z]{2})(?=\d)", "$1 ")

' 3) single-space around isolated two-uppercase tokens
text = Regex.Replace(text, "(?<![A-Z])([A-Z]{2})(?![A-Z])", " $1 ")

' 4) add double-space around exact 5-digit numbers
text = Regex.Replace(text, "(?<!\d)(\d{5})(?!\d)", "  $1  ")

Notes and pitfalls: run step (1) before step (4) so a 10-digit value isn’t split by the 5-digit rule. Step (2) reduces GN50033 -> GN 50033 so the later 5-digit rule can add its double spacing; test interactions on real samples because multiple passes can create extra spaces—either collapse unintended duplicates where appropriate, or do a tokenized rebuild for exact control.

If you’re on classic VB6, use VBScript.RegExp (no lookbehind); adapt patterns to captures like (^|[^0-9])([0-9]{10})([^0-9]|$) and rebuild with groups. For production, prefer the tokenizer approach (match \d+|[A-Za-z]+|[^\w\s]|\s+, then decide per token) to avoid order-dependent surprises and to make spacing rules explicit. Test thoroughly against representative lines before writing the file back.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.