How do I replace unwanted characters from a string using Reg Exp?

G

Guest

Hello:

I have a string, "Testing_!@#$%^&*()". It may have single and double
quotations as well.

I would like to strip all chararcters others than a-z, A-Z, 0-9 and the comma.

I came across the following snippet in the online help but the output does
not change at all:

Function CleanInput(ByRef strIn As String) As String
'returns a string after stripping out all nonalphanumeric characters
except @, - (a dash), and . (a period).
' Replace invalid characters with empty strings.
Return Regex.Replace(strIn, "[^\w\.@-]", "")
End Function

So if I pass in a string, "Testing_!@#$%^&*()", I get the same back.

Any help will be appreciated.

Thanks.

Venki
 
G

Guest

While I was waiting, I was able to figure it out.

with the regex string, "[`~!@#$%^&*()_\-=+\[\]\{\}\|;:'./<>?\b\t ]" I was
able to strip out all unwanted characters except a-z, A-Z, 0-9, comma, double
quote.

Venki
 
G

Guest

Hello:

I also wanted to strip out any double quotes. So I changed the regex to

"[`~!@#$%^&*()_\-=+\[\]\{\}\|;:'./<>?\b\t \34]"

since I cannot embed a double quote within a string in VB (or at least I do
not know how to.)

But this does not strip the double quotes. I checked the above with
RegexBuddy and it works with it.

Thanks

venki

vvenk said:
While I was waiting, I was able to figure it out.

with the regex string, "[`~!@#$%^&*()_\-=+\[\]\{\}\|;:'./<>?\b\t ]" I was
able to strip out all unwanted characters except a-z, A-Z, 0-9, comma, double
quote.

Venki

vvenk said:
Hello:

I have a string, "Testing_!@#$%^&*()". It may have single and double
quotations as well.

I would like to strip all chararcters others than a-z, A-Z, 0-9 and the comma.

I came across the following snippet in the online help but the output does
not change at all:

Function CleanInput(ByRef strIn As String) As String
'returns a string after stripping out all nonalphanumeric characters
except @, - (a dash), and . (a period).
' Replace invalid characters with empty strings.
Return Regex.Replace(strIn, "[^\w\.@-]", "")
End Function

So if I pass in a string, "Testing_!@#$%^&*()", I get the same back.

Any help will be appreciated.

Thanks.

Venki
 
H

Herfried K. Wagner [MVP]

vvenk said:
Function CleanInput(ByRef strIn As String) As String
'returns a string after stripping out all nonalphanumeric
characters
except @, - (a dash), and . (a period).
' Replace invalid characters with empty strings.
Return Regex.Replace(strIn, "[^\w\.@-]", "")
End Function

In addition to the other replies, pass 'strIn' as 'ByVal'. 'String' is a
reference type.
 
G

Guest

Herfriend:

I noticed that too. BTW, this is from the on-line help. Even after changing
it, the function, as it is written in the on-line help, does not work.

venki

Herfried K. Wagner said:
vvenk said:
Function CleanInput(ByRef strIn As String) As String
'returns a string after stripping out all nonalphanumeric
characters
except @, - (a dash), and . (a period).
' Replace invalid characters with empty strings.
Return Regex.Replace(strIn, "[^\w\.@-]", "")
End Function

In addition to the other replies, pass 'strIn' as 'ByVal'. 'String' is a
reference type.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top