string manipulation question

  • Follow


how would you do a search and replace like this?

<!-- start marker 1 -->

bunch o text

<!-- end marker 1 -->

let's say you want to replace what ever is between the start and end
markers with text from an external file.
would just like some smart tips how to do this with VB.

tia,
mcnewsxp

0
Reply mcnews 4/19/2010 2:27:05 PM

mcnews wrote:
> how would you do a search and replace like this?
> 
> <!-- start marker 1 -->
> 
> bunch o text
> 
> <!-- end marker 1 -->
> 
> let's say you want to replace what ever is between the start and end
> markers with text from an external file.
> would just like some smart tips how to do this with VB.

mid$() comes to mind...

Simplest would be if can be held in memory to read target into string 
variable, find start/end and replace.

Fastest if sizes are considerable could be the memmove() tricks (easily 
found by google here or on web as it's been discussed ad nauseum)...

--
0
Reply dpb 4/19/2010 2:38:12 PM


On Apr 19, 8:27=A0am, mcnews <mcour...@mindspring.com> wrote:
> how would you do a search and replace like this?
>
> <!-- start marker 1 -->
>
> bunch o text
>
> <!-- end marker 1 -->
>
> let's say you want to replace what ever is between the start and end
> markers with text from an external file.
> would just like some smart tips how to do this with VB.
>
> tia,
> mcnewsxp

Your question did not describe the source of the text, so I would
limit my suggestion just to the search & replace which is covered by
the replace statement:

 text1.text =3D replace(text1.text,"search text", "replace text",start
position, occurrence, compare type)

Finding the start position (start / end marker) can normally be
determined with the use of the InStr statement or omitting the start
position searches the entire value of text1.text

Duke
0
Reply duke 4/19/2010 3:22:07 PM

On Apr 19, 8:27=A0am, mcnews <mcour...@mindspring.com> wrote:
> how would you do a search and replace like this?
>
> <!-- start marker 1 -->
>
> bunch o text
>
> <!-- end marker 1 -->
>
> let's say you want to replace what ever is between the start and end
> markers with text from an external file.
> would just like some smart tips how to do this with VB.
>
> tia,
> mcnewsxp

My apologies.... I reread your question, you did say the text is
coming from an external file.

Anyways, you could apply the replace statement to the buffer area of
the file then write the contents back out to disk.

Duke
0
Reply duke 4/19/2010 3:25:12 PM

On Apr 19, 11:25=A0am, duke <nosp...@3web.net> wrote:
> On Apr 19, 8:27=A0am, mcnews <mcour...@mindspring.com> wrote:
>
> > how would you do a search and replace like this?
>
> > <!-- start marker 1 -->
>
> > bunch o text
>
> > <!-- end marker 1 -->
>
> > let's say you want to replace what ever is between the start and end
> > markers with text from an external file.
> > would just like some smart tips how to do this with VB.
>
> > tia,
> > mcnewsxp
>
> My apologies.... I reread your question, you did say the text is
> coming from an external file.
>
> Anyways, you could apply the replace statement to the buffer area of
> the file then write the contents back out to disk.
>
> Duke

that is pretty much what i am doing now except that i am just looking
for the first marker.  what would be the best way to grab the text
between to start and end markers?
0
Reply mcnews 4/19/2010 4:22:49 PM

"mcnews" <mcourter@mindspring.com> wrote
<...>
> that is pretty much what i am doing now except that i am just looking
> for the first marker.  what would be the best way to grab the text
> between to start and end markers?

Load the marked text into a string
Load the replaceing text from the file, into a string
Use Instr to find the first and last markers
Build a third string using the first part, file text, and last part of the marked text.

If you're talking several megabytes of text, send the output to a file,
rather than building the third string in memory....

LFS


0
Reply Larry 4/19/2010 5:08:40 PM

On Apr 19, 2:09=A0pm, "Larry Serflaten" <serfla...@usinternet.com>
wrote:
> "mcnews" <mcour...@mindspring.com> wrote
> <...>
>
> > that is pretty much what i am doing now except that i am just looking
> > for the first marker. =A0what would be the best way to grab the text
> > between to start and end markers?
>
> Load the marked text into a string
> Load the replaceing text from the file, into a string
> Use Instr to find the first and last markers
> Build a third string using the first part, file text, and last part of th=
e marked text.
>
> If you're talking several megabytes of text, send the output to a file,
> rather than building the third string in memory....
>
> LFS

i know how to do all but > Load the marked text into a string

am i reading from an input buffer until i see me 2nd marker?
0
Reply mcnews 4/19/2010 5:24:54 PM

mcnews wrote:
....

> i know how to do all but > Load the marked text into a string
> 
> am i reading from an input buffer until i see me 2nd marker?

Yet again, I say, Mid$ is your friend

Dim Char1 As String, Char2 As String ! For search char's (or Const)
Dim Str1 As String, MarkStr As String

Dim IdxStrt As Long, IdxEnd As Long

IdxStrt = Instr(Str1,Char1)  % First char pos'n in Str1
IdxEnd  = Instr(IdxStrt+1,Str1,Char2)  % Begin after first
! Do your error checking that both indices are nonzero...
MarkStr = Mid$(Str1,IdxStrt,IdxStrt-IdxEnd+1)

--
0
Reply dpb 4/19/2010 5:42:01 PM

On Apr 19, 1:42=A0pm, dpb <n...@non.net> wrote:
> mcnews wrote:
>
> ...
>
> > i know how to do all but > Load the marked text into a string
>
> > am i reading from an input buffer until i see me 2nd marker?
>
> Yet again, I say, Mid$ is your friend
>
> Dim Char1 As String, Char2 As String ! For search char's (or Const)
> Dim Str1 As String, MarkStr As String
>
> Dim IdxStrt As Long, IdxEnd As Long
>
> IdxStrt =3D Instr(Str1,Char1) =A0% First char pos'n in Str1
> IdxEnd =A0=3D Instr(IdxStrt+1,Str1,Char2) =A0% Begin after first
> ! Do your error checking that both indices are nonzero...
> MarkStr =3D Mid$(Str1,IdxStrt,IdxStrt-IdxEnd+1)
>
> --

with a little tweaking that works great!
thank ya.
0
Reply mcnews 4/19/2010 6:13:44 PM

dpb wrote:
....

> MarkStr = Mid$(Str1,IdxStrt,IdxStrt-IdxEnd+1)

woops...

MarkStr = Mid$(Str1,IdxStrt,IdxEnd-IdxStrt+1)

--
0
Reply dpb 4/19/2010 7:22:37 PM

9 Replies
206 Views

(page loaded in 0.129 seconds)

Similiar Articles:
















7/31/2012 4:02:12 AM


Reply: