Click here to Skip to main content
15,867,330 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can I check special characters are there (String) or Display on label?
I Use this but it is only count


how to display what special Chr are there in this string

What I have tried:

Dim input As String = "Hi! Hello, How are you?"
        Dim pattern As String = "[!""#$%&'()*+,\-./:;<=>?@[\\\]^_`{|}~\s]"
        Dim matches As MatchCollection = Regex.Matches(input, pattern)
        MessageBox.Show(String.Format("There are {0} special characters in the input '{1}'", matches.Count, input))
Posted
Updated 6-Apr-22 2:26am
v2

Your regex pattern is wrong.

Use this: [!""#$%&'()*+,\-.\/:;<=>?@[\\\]^_`{|}~\s]

In other words: / An unescaped delimiter must be escaped with a backslash (\)
 
Share this answer
 
Comments
Jayanta Modak 6-Apr-22 12:27pm    
actually I want to display in msgbox what is the special character
exm: "Hi! Hello, How are you?"
Result : ! , ?
Maciej Los 6-Apr-22 15:38pm    
You just need to loop through the collection of matches:
For Each m As Match In matches
	MessageBox.Show(String.Concat("Found: '", m.Value, "'"))
Next m
Jayanta Modak 7-Apr-22 22:40pm    
Thanks sir, Love you to. You solve my problem, thanks again because sir you understand my problem means actually what I need THANKS SIR
Maciej Los 8-Apr-22 0:20am    
You're very welcome.
Quote:
How to get special characters from a string in VB.NET

Those links should help you to build and debug RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx: Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx: Regexper[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900