mattrupp
mattrupp

Reputation: 1

Search SubStrings between defined Strings or at the end of defined strings with Vbscript compatible Regular expression

I do have to parse specific Information from Report headers in Text Format
Since I do have to perform this through Regular expressions in VB Script I cannot make use of the Look Ahead features of Regex.

Typical use cases to cover for Regular expressions would be

1) User Name : Clark Kent
Extracted by Regex: "Clark Kent" for any line Starting with "User Name :"
2) User Name Clark Kent
Extracted by Regex: "Clark Kent" for any line Starting with "User Name" and delete all leading and trailing blanks from " Clark Kent "
3) User Name: Clark Kent Sample ID : 1234
Extracted by Regex: "Clark Kent" from any line starting with "Clark Kent" and Ending with "SampleID :"

Any Help for this example would be highly appreciated

Upvotes: 0

Views: 46

Answers (1)

Bond
Bond

Reputation: 16321

My regex skills aren't the greatest but this pattern works for your three examples. It currently only looks for and returns <first name><space><last name>, but it could be adapted to handle more complicated scenarios.

With New RegExp
    .Pattern = "^User Name\s*:?\s*(\w+\s\w+)\s*(?:Sample ID)?"
    Set Matches = .Execute(strLine)
End With

If Matches.Count > 0 Then strName = Matches(0).SubMatches(0)

Upvotes: 1

Related Questions