Link to home
Start Free TrialLog in
Avatar of yababy
yababy

asked on

Parsing Exact Matches In A Text File.

How do you find exact matches in string using VB6?

Example: "This is the best site in the internet"

I need to find the word "in" so it should only match in position 24. As you can see we have another position also in the word "internet". Using Instr we return two matches not one.

Thanks
Avatar of DrDelphi
DrDelphi

Well, the easiest way if you are looking to get a full word match and not a substring would be to pad yourd search with a leading a trailing space:


For example:

ret = InStr("This is the best site in the internet", " in ")

Good luck!!


Avatar of yababy

ASKER

Yes this would work by what if the word is at the beginning or end of a string. Does VB have any other string functions?

Private Sub Command1_Click()
Dim X As String
Dim Y As Integer

X = "This is the intro day we are in into town"
Y = InStr(X, "in ")
Text1.Text = Y
End Sub


In this case "in" is not just "in"
but has a space before it and a space after it so rather just to search for  in by itself we search for "in "
which will return the correct position
You can do what Dr. Delphi says.

Another method:

ret = InStr(1, "This is the best site in the internet", "in")

In this example, 1 indicates that the search for "in" is to begin at position 1 of "This is the best site in the internet".
Use,

ret = InStr(" " & "This is the best site in the internet" & " ", " in ")

This will look for it at the begining and end as well as in the middle...

Use,

ret = InStr(" " & "This is the best site in the internet" & " ", " in ")

This will look for it at the begining and end as well as in the middle...

or:

Mystr = "dog My  has Fleas"
ret = InStr(Mystr, "dog")
If ret = 1 Then
  MsgBox ("begining of string")
Else

If ret = (Len(Mystr) - 2) Then
  MsgBox ("Last word")
Else
If ((ret > 0) And ret < Len(Mystr)) Then
  MsgBox ("somewhere in the middle")
End If
End If
End If
ASKER CERTIFIED SOLUTION
Avatar of caraf_g
caraf_g

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dragonwolf - I purposely used a sentence that ended in "in." to illustrate why your method won't work.
PS - if your language can contain accented characters, include them in the if statements. That extension of my code is trivial, so I've left that out for you to do.
Avatar of yababy

ASKER

Thanks, but I will be using a new object called RegExp (VBScript Regular Expressions) which works great for finding exact matches.