473,748 Members | 3,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ByRe f 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(s trIn, "[^\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 3027
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(ByRe f 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(s trIn, "[^\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(ByRe f 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(s trIn, "[^\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***@discussi ons.microsoft.c omschrieb:
Function CleanInput(ByRe f 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(s trIn, "[^\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***@discussi ons.microsoft.c omschrieb:
Function CleanInput(ByRe f 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(s trIn, "[^\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
4063
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 problems getting my algorithm to work and in order to help me find the solution I decided to print each line to screen as I read them. Then, to my surprise, I noticed that there was a space between every character as I outputted the lines to the...
23
6996
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 return/line feed" or "line feed/carriage return" and removing them. I quess what I need is an editor that allows non-printable characters in it's search strings. Does anyone know of on that will allow this? Thank you...
13
11025
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 below would work... string gsub(const string & sData, const string & sFrom,
3
30409
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 have a string consisting of: -- 3 spaces -- The characters "hello"
3
1450
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 assume that I'm not familiar w/ the structure of the XML so that I'll have to read/replace/load via stream. Is this possible OpenStream->Replace char(s)->To XML parser ? If so then how? Art
16
2796
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? As shown below line #21, I should remove the unwanted characters in input stream there at that time. Do I miss some other possible errors in i/o which will happen to occur sometimes in other places? And welcome your kind comments on following the...
7
3793
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. The list also needs to be editable by the end-user so I'll be providing a form on which they can edit the allowed character list. The end-user is non-technical so asking them to type a regular expression is out.
1
3629
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 that means that the unicode number in base 10 is 194. So I want to do a String.Replace(mystr,194,''c) But how do I convert the 194 into a Char, and how do I replace that by blanks. Thanks, Marvin
0
1374
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
8987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8826
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9241
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8239
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6793
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
2
2777
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2211
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.