How to replace a string that is enclosed for a couple of tags

HI

Now I have a string like this "start-blablablablablabla-end" in a file, I need to replace the whole string but the deal is that the string between "start-" and "-end", is not always the same (content and size).

Regards
dimensionavAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VBClassicGuyCommented:
Assume your text string is in the declared text variable "txt".

Dim NewText As String

If Left(txt, 6) = "start-" And Right(txt, 4) = "-end" Then
   'do your string substitution here
   NewText = <whatever>
End If
0
slightwv (䄆 Netminder) Commented:
I would look at regular expressions.

My straight VB is pretty rusty but check out:
http://www.vb-helper.com/howto_regular_expression_replace_lines.html

You would want to replace:  "(start-).*(-end)" with whatever you needed.


>>If Left(txt, 6) = "start-" And Right(txt, 4) = "-end" Then

From the related question I believe that string can exist anywhere in a larger string.
0
Muhammad KhanManager, ITCommented:
Sub Macro1()
Set fso = CreateObject("Scripting.FileSystemObject")
Dim fileStream
Dim s
Dim ifrom
Dim ito
Dim sToReplace
Dim sToBeReplaced

Set fileStream = fso.OpenTextFile("c:\workshop\File1.txt", 1)
sToReplace = "just a test string"

s = fileStream.ReadAll
ifrom = InStr(1, s, "start-", vbTextCompare) + Len("start-")
ito = InStr(1, s, "-end", vbTextCompare)
sToBeReplaced = Mid(s, ifrom, ito - ifrom)
s = Replace(s, sToBeReplaced, sToReplace)

fileStream.Close
Set fileStream = fso.OpenTextFile("c:\workshop\File1.txt", 2)
fileStream.Write (s)
fileStream.Close
End Sub
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
Const TESTDATA = "start-blablablablablabla-end"

Dim intStart As Integer
Dim intEnd As Integer

intStart = InStr(TESTDATA, "start-")
intEnd = InStr(TESTDATA, "-end")

Debug.Print Mid$(TESTDATA, intStart + Len("start-"), intEnd - intStart - Len("start-"))

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.