Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove all strings within brackets (and the brackets themselves) 1

Status
Not open for further replies.

slobad23

IS-IT--Management
Jun 30, 2006
90
GB
I have various fields that are coming back in a report I am running and have the option of running vbscript against them.

What I want to do is if there is anything contained within brackets, I want it to remove it for me.

Could someone please help me with this.

Example

"Telephone number: 12345 678910 (international)" - I would want the "(international)" bit removed.

Thanks.
 
A starting point:
myText="Telephone number: 12345 678910 (international)"
iStart=InStr(1,myText,"(")
iEnd=InStr(iStart,myText,")")
MsgBox Left(myText,iStart-1) & Mid(myText,iEnd+1)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
An alternative is to use regular expressions:
Code:
[COLOR=green]'[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/ms974570.aspx[/URL]
'[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/yab2dx62.aspx[/URL]
'[URL unfurl="true"]http://www.regular-expressions.info/index.html[/URL][/color]

strIn = "Telephone number: 12345 678910 (international) more stuff (get rid of this)"
strOut = CleanString(strIn)
'do something with the new string
wscript.echo("Input: " & strIn & vbcrlf & "Output: " & strOut)

Function CleanString(strInput)

[COLOR=green]'create regular expression object[/color]
set re = new regexp
[COLOR=green]'set the pattern to match: any number of characters contained within parentheses, including the parentheses[/color]
re.Pattern = "\(.*?\)"
re.IgnoreCase = True
[COLOR=green]'match all instances found[/color]
re.Global = True

CleanString = re.Replace(strInput, "")

set re = nothing

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top