Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding text in a text file, and find next

Status
Not open for further replies.

sarcus25

Programmer
Apr 14, 2005
27
Hi I am trying to find and select text and copy it from a text box. I can do that no problem but I can't seem to do a find next, the same line of text always gets selected even though it is not the only matching string. Thanks in advance.


Dim StartText, EndText, StartTag, EndTag
StartTag = "modMovieID="
EndTag = "ModID="

'Do Until Y1 = 5
X1 = 1

Y1 = Y1 + 1

If Y1 > 1 Then
X1 = StartText + 2500
End If

'Getting the start and end position of the text to be selected
'==============================================================
StartText = InStr(X1, txtSource.Text, StartTag)
'10

'EndText = InStr(txtSource.Text, EndTag)

'10
'Selects the text specifed above acording to position values above
'==================================================================
If StartText Then
txtSource.SelStart = StartText - 1
txtSource.SelLength = 44
Else
MsgBox "String Not Found"
End If

txtSource.SetFocus


'Copying selected text tothe clipboard
==========================================
'Clear the contents of the Clipboard.
Clipboard.Clear
' Copy selected text to Clipboard.
Clipboard.SetText Screen.ActiveControl.SelText

'Loop
 
You need to set X1 to a position greater than the position where you found the first occurence before you use instr to locate the second occurence. Unless your first parameter is greater than the location of the first occurence, you'll keep finding that occurence.

You could do this same thing much easier with the regular expression execute method. It will give you ALL occurences that it found all at once.

Here's an example from the MSDN web site (
Code:
Function RegExpTest(patrn, strng)
   Dim regEx, Match, Matches   ' Create variable.
   Set regEx = New RegExp   ' Create a regular expression.
   regEx.Pattern = patrn   ' Set pattern.
   regEx.IgnoreCase = True   ' Set case insensitivity.
   regEx.Global = True   ' Set global applicability.
   Set Matches = regEx.Execute(strng)   ' Execute search.
   For Each Match in Matches   ' Iterate Matches collection.
      RetStr = RetStr & "Match found at position "
      RetStr = RetStr & Match.FirstIndex & ". Match Value is '"
      RetStr = RetStr & Match.Value & "'." & vbCRLF
   Next
   RegExpTest = RetStr
End Function
MsgBox(RegExpTest("is.", "IS1 is2 IS3 is4"))

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Hmmm tried to use this Function but visual basic 6 does not know RegExp. Is their something that I'm doing wrong.
 
u need to either add a reference to regular expressions library
(project->references : microsoft vbscript regular expressions x.x)

or declare as objects

Dim RE As Object
Set RE = CreateObject("VBScript.RegExp")

good luck

If somethings hard to do, its not worth doing - Homer Simpson
 
Hmm. I don't have to do it that way. Once I set the reference all I do is:
Code:
  Dim regex As RegExp
  Set regex = New RegExp
  regex.Global = True
  regex.IgnoreCase = True
  regex.MultiLine = True
  regex.Pattern = "[i]test-pattern[/i]"
  regexTest = regex.Test("[i]test-string[/i]")


Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Hi again I have tried the code you listed tsdragon but again I'm still getting only the first occurence of the string that i am searching for. It always displays a position of 937 and won't list the position of all other matches for this search. Any idea of what i will have to do to make this work. Thanks in advance.





 
opps found out what i was doing wrong, you code is good. Thanks very much.








Doh!!!!----Homer Simpson
 
Glad you figured out the problem. You really should read up on regular expressions. They're incredibly useful in many situations. Besides the extracting like you're doing they're also good for testing for patterns in a string and for doing search-and-replace.

The link I gave above is a good place to start.

Tracy Dryden

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top