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!

strip escaped chars 2

Status
Not open for further replies.

Geates

Programmer
Aug 25, 2009
1,566
US
is there a vbs function or a combination of functions that would remove all special chars from a string; leaving just literals. What about a regex solution?

before:
"\t20 dwarfs took turns doing\n handstands on the carpet\r"

after:
"20 dwarfs took turns doing handstands on the carpet"

This is what I'm using now
Code:
function bare(str)
	for i =  1 to len(str)
		char = mid(str, i, 1)
		if (((asc(char) >= 32) and (asc(char) <= 126)) and (asc(char) <> 92)) then
			 if (asc(prevChar) <> 92) then str2 = str2 & char
		end if
		prevChar = char
	next
	bare = str2
end function

-Geates
 
You might try using the INSTR() function to find the first instance of the character and it's position, then remove it and try again until INSTR() returns 0.

Dave.
 
Good suggestion. Much faster!

Code:
function bare(str)
	intPos = inStr(str, "\")
	do while (intPos)
		esc = mid(str, intPos, 2)
		str = replace(str, esc, "")
		intPos = inStr(intPos + 1, str, "\")
	loop
	bare = str
end function

-Geates

 
And here's one possible regex solution:

Code:
[COLOR=green]'[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/ms974570.aspx[/URL][/color]
[COLOR=green]'[URL unfurl="true"]http://msdn.microsoft.com/en-us/library/yab2dx62.aspx[/URL][/color]
[COLOR=green]'[URL unfurl="true"]http://www.regular-expressions.info/index.html[/URL][/color]
[COLOR=blue]Option[/color] [COLOR=blue]explicit[/color]  
[COLOR=blue]dim[/color] strInput, strOutput, re  

[COLOR=blue]set[/color] re [COLOR=blue]=[/color] [COLOR=blue]new[/color] regexp  

strInput [COLOR=blue]=[/color] "\t20 dwarfs took turns doing\n handstands on the carpet\r"  

re.Pattern [COLOR=blue]=[/color] "\\[a-z]{1}"  
re.IgnoreCase [COLOR=blue]=[/color] [COLOR=blue]true[/color]  
re.Global [COLOR=blue]=[/color] [COLOR=blue]true[/color]  
wscript.echo("before cleanup: " [COLOR=blue]&[/color] strInput)  
[COLOR=blue]if[/color] re.Test(strInput) [COLOR=blue]then[/color]  
	strOutput [COLOR=blue]=[/color] re.Replace(strInput, "")  
end [COLOR=blue]if[/color]  

wscript.echo("after cleanup: " [COLOR=blue]&[/color] strOutput)
 
jges,

I hadn't seen that before, very cool.

Thanks,
Dave.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top