1

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"?

7
  • 2
    Please post a code attempt of yours. Also consider using a service like rubular.com to try it yourself. Commented Nov 30, 2012 at 3:58
  • hi Sunny, thanks for sending the link rubular.com, it's very good. I had tried gged\b it matches logged and i put \w (username) it matches username but how do i combined those two commands? Thanks. Commented Nov 30, 2012 at 4:21
  • You can combine the command using "OR" which looks like |. Commented Nov 30, 2012 at 4:32
  • 1
    Did you try with UserName.*Logged on ? Commented Nov 30, 2012 at 9:11
  • 2
    Use the pattern ^This (.+) is (.+) already\.$ on a Global search. Capture the matches. Username should be in matches(0).submatches(0), Logged on in matches(0).submatches(1). Commented Nov 30, 2012 at 15:21

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.