Solved

VBScript code to delete any data between two characters

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
I met Paul Devereux (@pdevereux) today when I responded to his tweet asking “Anybody know how to automate adding files from disk to a folder in #outlook  ?”.  I replied back and told Paul that using automation, in this case scripting, to add files t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now