473,320 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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
Oct 7 '06 #1
4 3004
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

Oct 7 '06 #2
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" 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
Oct 7 '06 #3
"vvenk" <vv***@discussions.microsoft.comschrieb:
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/>

Oct 7 '06 #4
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 [MVP]" wrote:
"vvenk" <vv***@discussions.microsoft.comschrieb:
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/>

Oct 8 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Eric Lilja | last post by:
Hello, I had what I thought was normal text-file and I needed to locate a string matching a certain pattern in that file and, if found, replace that string. I thought this would be simple but I had...
23
by: SeaPlusPlus | last post by:
I want to convert large files of prose to xhtml and so I need a way to remove unwanted line wraps. So, I'm looking for a freebee editor that has the capability of searching for a single "carriage...
13
by: M | last post by:
Hi, I've searched through the previous posts and there seems to be a few examples of search and replacing all occurrances of a string with another string. I would have thought that the code...
3
by: o_swas | last post by:
Hello, I have a JavaScript string. I want to replace all consecutive occurrences of whitespace characters like spaces, tabs, newlines, and form feeds with another string. For example, say I...
3
by: Art | last post by:
What's the most efficient way to replace characters in an XML document before it is loaded into a parser? Chars I'd want to replace are in attributes and there can be N attributes, also let's...
16
by: lovecreatesbeauty | last post by:
/* When should we worry about the unwanted chars in input stream? Can we predicate this kind of behavior and prevent it before debugging and testing? What's the guideline for dealing with it? ...
7
by: Grok | last post by:
I need an elegant way to remove any characters in a string if they are not in an allowed char list. The part cleaning files of the non-allowed characters will run as a service, so no forms here. ...
1
by: COHENMARVIN | last post by:
I have a string with an unwanted unicode character. It looks like an 'A' with a tilde on top. I looked up a unicode chart on the internet and the chart says that its represented by  So I think...
0
by: Alexey Smirnov | last post by:
On Jul 10, 11:03 pm, Alexey Smirnov <alexey.smir...@gmail.comwrote: Sorry, the one with a tilde on top is Â
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.