Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
.*([a-zA-Z0-9]){2}.* is the regular expression to validate atleast 2consicutive alpha numerics are present

How to check special characters are present using the same regular expression

What I have tried:

I have used 2 regular expression 1 for special char check and the second for checking 2 consecutive alpha numeric
Posted
Updated 8-Sep-17 13:54pm
Comments
Patrice T 8-Sep-17 2:11am    
What is 'special character' ?
Member 12613458 8-Sep-17 3:17am    
@#$%^&*()_+

It depends on how the conditions are connected (OR, AND, AND with specific order).

2 alnum or at least one special character:
([a-zA-Z0-9]{2})|([@#$%^&*()_+]+)

2 alnum followed somewhere by at least one special:
[a-zA-Z0-9]{2}.*[@#$%^&*()_+]+

There is no general solution for a plain AND without knowing the used regex engine. You might read about lookahead and how that is implemented for the used engine. A possible (untested) solution:
(?=[^@#$%^&*()_+]*[@#$%^&*()_+])(?=(?:[^a-zA-Z0-9]*[a-zA-Z0-9]){2})

You can still use two conditions. Then the special char check might be implemented by using a corresponding string function (FindAny, IndexOfAny, strpbrk) instead of a regex.
 
Share this answer
 
You didn't described what validation you want, just some chars.
.*([a-zA-Z0-9@#$%^&*()_+]){2}.* can be what you want ... or not, we can't know.

Just a few interesting links to help building and debugging 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[^]
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.[^]
 
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