Finding Text and Inserting Text Please Help

Dereck21 used Ask the Experts™
First off I would like to thank everyone in advance, as I do know somethings about Visual Basic, I am mainly self taught and I admire all of your knowledge.  Heres my situation:

I have a text file about 100 pages long of pure non stop text, numbers, and symbols.  They are record extracts from a database.  I need a program that will go through the text file and after a certain text "text1" copy the digits between "text1" and "text2".  Then it must paste what it copied after the very next "text3".  Then if must move on until it finds "text1" again which will be a few lines further down and repeat the whole process.  "text1", "text2" and "text3" are contants.  However the number of characters that it needs to copy and paste will change each time.

Any help is greatly appretiated.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
There are several approaches to this.  Here's one:

' split contents at the first "marker"
Dim strEntryArray() as string
strEntryArray = split(strFileContents, "text1")

' Repeat this for all items
dim iEntryCntr as integer
for iEntryCntr = 0 to ubound(strEntryArray)

' Now pull off everything before the first "text2"
dim lngIndex as long
lngIndex = instr(strEntryArray(iEntryCntr), "text2")

if lngIndex> 0 then
  strDigitsOfText = left$(strEntryArray(iEntryCntr), lngIndex-1)

  ' locate the next text and insert it:
  lngIndex = instr(lngIndex+1, strEntryArray(iEntryCntr), "text2")
  if lngIndex > 0 then
    ' insert it
    strEntryArray(iEntryCntr) = left$(strEntryArray(iEntryCntr),lngIndex -1 ) & strDigitsOfText  & mid$(strEntryArray(iEntryCntr),lngIndex)
    msgbox "text3 not found in item # " & iEntryCntr
  msgbox "text2 not found in item # " & iEntryCntr

next iEntryCntr

' re-assemble the pieces
dim strNewData as string

strNewData = join(strEntryArray, "text1")

I didn't test it, but it should be close

I would solve this with regular expressions...

Lemme slap an example together :)

Here it is :

Private Sub Command1_Click()
Dim myRegExp As New RegExp
Dim myMatches As MatchCollection
myRegExp.Global = True   'whole text not just 1st time
myRegExp.IgnoreCase = True 'ignore case
myRegExp.Pattern = "(text1)([0-9]*)(text2)(.*)(text3)" 'Ok find some place that holds text1 , followed by some digits, followed by text2, followed by anything, followed by text3
Set myMatches = myRegExp.Execute(RichTextBox1.Text)
Debug.Print myMatches.Count
RichTextBox1.Text = myRegExp.Replace(RichTextBox1.Text, "$1$2$3$4$5$2")
End Sub

Some remarks about this string :
(text1) = your 1st token you wish to find (refference in replace as $1)
([0-9]*) - a number that has unlimit digits not just 2 ;) - but also 0 digits ... * can be replaced by + to match more then one digit - this is $2 in the replace string
(text2) = your 1st token you wish to find (refference in replace as $3)
(.*) = anything between text2 and text3 . is a wildcard and * a multiplayer (zero or more) again -> $4
(text3) = your 1st token you wish to find (refference in replace as $5) The token where you want to insert stuff after

Hope this helps

Some things you might want to add to the string:

-it expects NOTHING between text1 and the number so it wont find "text1 234" it will only find "text1234" - fill in what really is there ;)
-same about text2 -> "234text2" will match and "234 text2" wont match
-(.*) this will only match everything on that line i THINK
(to match more then one line you need the latest msScripting runtime) - my pc only has the "old" regexp here . it has no multiline property)

Hope this helps ;)


Thanks, forgot about awarding the points!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial