Replacing non-alphanumeric characters in a string

R

Robert Brown

Hi All

I have searched high and low but there seems nobody wants to do this..

For instance, I have a textbox that represents a Purchase Order Field.
Some users like typing non alphanumeric characters into this field.

What I was hoping is that I can use the replace function to set
anything not in a filter to ""

Ok, an example:

String typed in: #=98776@

String needed: 98776

I have seen one example for asp.net to use a filter in the replace
function, but this does not work in vb.net.

This is the line I tried:

sPOrder = Replace(sPOrder, "^.*[^A-Za-z0-9].*$", "")

And I have tried other variations of the above, but the desired result
is never returned.

Can anyone show an example of how this should work?

Thankyou All...

Robert
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Robert Brown) scripsit:
For instance, I have a textbox that represents a Purchase Order Field.
Some users like typing non alphanumeric characters into this field.

What I was hoping is that I can use the replace function to set
anything not in a filter to ""

Ok, an example:

String typed in: #=98776@

String needed: 98776

\\\
Private Function StripNonAlphaNumericCharacters(ByVal Text As String) As String
Dim sb As New System.Text.StringBuilder(Text.Length)
Dim chr As Char
For Each chr In Text
If Char.IsLetterOrDigit(chr) Then
sb.Append(chr)
End If
Next chr
Return sb.ToString()
End Function
///
 
C

Cor Ligthert

Herfried,

In this I go for the solution from Brian and not the one from you or me,
this is typical regex.

:)

Cor
 
J

Jeff Johnson [MVP: VB]

I have seen one example for asp.net to use a filter in the replace
function, but this does not work in vb.net.

This is the line I tried:

sPOrder = Replace(sPOrder, "^.*[^A-Za-z0-9].*$", "")

For reference, what you have asked to do above is "as long as the string
contains a non-alphanumeric character, match the ENTIRE string and replace
it with an empty string." The reason for this is the ^.* and .*$ parts, and
that's why Brian left them out of his solution.
 
H

Herfried K. Wagner [MVP]

* "Cor Ligthert said:
In this I go for the solution from Brian and not the one from you or me,
this is typical regex.

Would be interesting which one is faster for very long strings...
 
C

Cor Ligthert

Would be interesting which one is faster for very long strings...

I would be very much suprissed when it was not ours, however that is not
always the most important thing

(And first decoding it to a bytearray what is allowed in this situation
would even make that twice as fast)

:)

Cor
 
R

Robert Brown

Thankyou to all that replied..

I have two questions, then I will leave you alone..

1) Is Regex.Replace the same as the normal Replace routine
2) Is there anywhere that explains how to use the filters or correct
syntax in the regex.rplace routine (eg [^A-Za-z0-9])? I did look at
the articles given, but I couldn't find anywhere that explained it.

Thanks all again for the fast replies.

Robert




Jeff Johnson said:
I have seen one example for asp.net to use a filter in the replace
function, but this does not work in vb.net.

This is the line I tried:

sPOrder = Replace(sPOrder, "^.*[^A-Za-z0-9].*$", "")

For reference, what you have asked to do above is "as long as the string
contains a non-alphanumeric character, match the ENTIRE string and replace
it with an empty string." The reason for this is the ^.* and .*$ parts, and
that's why Brian left them out of his solution.
 
H

Herfried K. Wagner [MVP]

* (e-mail address removed) (Robert Brown) scripsit:
1) Is Regex.Replace the same as the normal Replace routine
No.

2) Is there anywhere that explains how to use the filters or correct
syntax in the regex.rplace routine (eg [^A-Za-z0-9])? I did look at
the articles given, but I couldn't find anywhere that explained it.

A good ressource about regular expressions can be found here (in
addition to the .NET 'Regex' and Regular Expressions documentation):

<URL:http://www.regular-expressions.info/>
 

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