TangoStar
TangoStar

Reputation: 551

Extracting a special part of a text using RegularExpression

I want to find a special part of a text using Regular Expressions. For example I have a text like: KENNFELD TFSWNWRSA 4 4

I want to extract only TFSWNWRSA 4 4 from this text and not KENNFELD and then I want to

I wrote this code but it returns all total row:

Dim fso As New FileSystemObject
Dim ts As TextStream
Dim Name As String
Dim regx As New regexp
Dim matchkennfeld As MatchCollection
Dim matchname As MatchCollection

 Name = "D:/test_DC.txt"
 'Set regexp = CreateObject("vbscript.regexp")
 Set ts = fso.OpenTextFile(Name, ForReading)
 Do While Not ts.AtEndOfStream
  regx.Pattern = "KENNFELD\s+([A-Z 0-9]*)"
  Set matchkennfeld = regx.Execute(ts.ReadLine)
  If matchkennfeld.Count <> 0 Then
     regx.Pattern = "([A-Z 0-9]*)"
    ' MsgBox matchkennfeld.Item(0)
     Set matchname = regx.Execute(matchkennfeld.Item(0))
        For Each Match In matchname
           MsgBox Match
        Next Match
 End If
 Loop

would you please help me to do this job?

Upvotes: 0

Views: 77

Answers (1)

Jithin
Jithin

Reputation: 2604

I am not good in VB. But I would say matchkennfeld will be an array, which contain the matches along with the group (It is like that in other languages). So when checking Item(0) I think its matching with the whole match, not the group. So changing it to check with Sub matches will fix the issue.

Set matchname = regx.Execute(matchkennfeld(0).SubMatches(0)) may fix the issue.

Upvotes: 1

Related Questions