|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How do I replace unwanted characters from a string using Reg Exp?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 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 > > 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 > > > > "vvenk" <vv***@discussions.microsoft.com> schrieb: In addition to the other replies, pass 'strIn' as 'ByVal'. 'String' is a > 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 reference type. -- M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/> 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/> > >
VB 6 developers and .Net
Read legacy vb5 files with variant declare in vb.net Running an Access macro from a VB.NETprogram How to prevent Pasting into textbox? How to assign value to a dropdown box items? ADOX add bool field efficient routine to parse a text string Memory problem with vb.net + webServices + DLL clicking on a DataGridView's DataSource property throws exception in designer keydown event not working problem |
|||||||||||||||||||||||