?
Solved

Parsing Exact Matches In A Text File.

Posted on 2000-02-20
11
Medium Priority
?
166 Views
Last Modified: 2010-05-02
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
0
Comment
Question by:yababy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2541255
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!!


0
 

Author Comment

by:yababy
ID: 2541258
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?

0
 
LVL 1

Expert Comment

by:okcman
ID: 2541269
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
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Ruchi
ID: 2541271
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".
0
 
LVL 1

Expert Comment

by:DragonWolf
ID: 2541272
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...

0
 
LVL 1

Expert Comment

by:DragonWolf
ID: 2541275
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...

0
 
LVL 6

Expert Comment

by:DrDelphi
ID: 2541307
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
0
 
LVL 10

Accepted Solution

by:
caraf_g earned 150 total points
ID: 2541623
Use the following routine:

Private Sub FindExactMatch(ByVal strToFind As String, ByVal strToSearch As String, intPos As Integer)

Dim strTest As String
Dim blnValid As Boolean

intPos = InStr(intPos + 1, strToSearch, strToFind)

Do While intPos <> 0
    blnValid = True
    If intPos <> 1 Then
        strTest = Mid(strToSearch, intPos - 1, 1)
        If strTest >= "a" And strTest <= "z" Then
            blnValid = False
        End If
        If strTest >= "A" And strTest <= "Z" Then
            blnValid = False
        End If
    End If
    If intPos <> Len(strToSearch) Then
        strTest = Mid(strToSearch, intPos + Len(strToFind), 1)
        If strTest >= "a" And strTest <= "z" Then
            blnValid = False
        End If
        If strTest >= "A" And strTest <= "Z" Then
            blnValid = False
        End If
    End If
    If blnValid Then
        Exit Sub
    End If
    intPos = InStr(intPos + 1, strToSearch, strToFind)
Loop

End Sub


Let's say you're searching the following string

"The internet is in use by all the countries it is implemented in."

for the string

"in"

It will find "in" in "is in use", and "in" in "implemented in."

But it won't find internet, as the "in" in internet is followed by an alphabetic character.

A possible use:

Dim intPos As Integer

FindExactMatch Text1.Text, Text2.Text, intPos
Do While intPos <> 0
    MsgBox intPos
    FindExactMatch Text1.Text, Text2.Text, intPos
Loop
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2541624
Dragonwolf - I purposely used a sentence that ended in "in." to illustrate why your method won't work.
0
 
LVL 10

Expert Comment

by:caraf_g
ID: 2541627
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.
0
 

Author Comment

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

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 4 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question