user1630482
user1630482

Reputation: 11

vbscript regex match two string

How do I use vbscript to find two texts that match within a singe line? For example:

This UserName is Logged on already.

How do I search for "UserName" and "Logged on"?

Upvotes: 1

Views: 2902

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200233

Regular expressions are probably overkill in this case. I'd suggest using InStr() for this kind of check:

s = "This UserName is Logged on already."
If InStr(s, "UserName") > 0 And InStr(s, "Logged on") > 0 Then
  '...
End If

You can wrap InStr() in a helper function if you want to make the check a bit better readable:

s = "This UserName is Logged on already."
If Contains(s, "UserName") And Contains(s, "Logged on") Then
  '...
End If

Function Contains(str1, str2)
  Contains = False
  If InStr(str1, str2) > 0 Then Contains = True
End Function

Upvotes: 3

Related Questions