Home All Groups Group Topic Archive Search About

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

Author
7 Oct 2006 7:25 PM
vvenk
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

Author
7 Oct 2006 8:43 PM
vvenk
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

Show quoteHide quote
"vvenk" wrote:

> 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
>
>
Author
7 Oct 2006 9:29 PM
vvenk
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

Show quoteHide quote
"vvenk" wrote:

> 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" wrote:
>
> > 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
> >
> >
Author
7 Oct 2006 10:12 PM
Herfried K. Wagner [MVP]
"vvenk" <vv***@discussions.microsoft.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
Author
8 Oct 2006 1:01 AM
vvenk
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

Show quoteHide quote
"Herfried K. Wagner [MVP]" wrote:

> "vvenk" <vv***@discussions.microsoft.com> schrieb:
> > 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.
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://dotnet.mvps.org/dotnet/faqs/>
>
>