Solved

VBScript code to delete any data between two characters

Posted on 2009-05-14
4
615 Views
Last Modified: 2012-05-07
I'm completely stumped on how to do this.

I have a file full of html code. I need to search through it and remove some code. The problem I have is that there is variable between [ and ], and I am not sure how to go about this.

I'm use to using replace and variables etc at a basic level, but this is well beyond me!

example code:
Stuff I need to keep<br /><font color="#ff0000">[variable data]</font><br /><font color="#000000">More stuff here I need to keep


I need a script/function to search and delete, leaving me with:
Stuff I need to keep<br /><br /><font color="#000000">More stuff here I need to keep

The square brackets don't appear anywhere else in the file, so they can be used as an anchor point for the deletion.
0
Comment
Question by:Burns2007
[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
  • 2
4 Comments
 
LVL 7

Expert Comment

by:BarryTice
ID: 24394601
First, read your text file into a single string variable, say, strText.
Then, try something like this:
Do While InStr(strText, "[") > 0
  intLeft = InStr(strText, "[")
  strLeft = Left(strText, intLeft - 23)
  intRight = InStr(strText, "]")
  strRight = Right(strText, Len(strText) - strRight - 7)
  strText = strLeft & strRight
Loop
 
This could be optimized somewhat for speed and memory usage, but this is nice and clear.

Open in new window

0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24395575
Hi,

Try this as well.

Pass the text into strText and it'll remove all the of [text] portions.
While InStr(strText,"[") > 0
	intOpen = InStr(strText,"["):intClose = InStr(strText,"]")
	strText = Replace(strText,Mid(strText,intOpen,(intClose-intOpen)+1),"")
Wend

Open in new window

0
 
LVL 27

Expert Comment

by:bluntTony
ID: 24395598
Barry, i couldn't personally get yours to work. What are the 23 and 7 for?

Not saying it's wrong but I like to get my head round other methods.
0
 
LVL 1

Accepted Solution

by:
maytan80 earned 485 total points
ID: 24397349
Use the split method. It will create an array on the variable

so x<y>z

I would do a split on "<" character giving me a 2 record array x and y>z
then do a split on > giving me x in the first array and y and z in the second array

Now mix and match

character array m = split(str, character/delimiter)
0

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

688 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