Solved

VBScript code to delete any data between two characters

Posted on 2009-05-14
4
599 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
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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