Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
”[A-Za-z0-9_@./#&+-\/s]{0,28}”/>
but this allows white space but doesn't reject name having more then 28 char rather it truncates that and allows only 28 char for the same .

What I have tried:

”[A-Za-z0-9_@./#&+-\/s]{0,28}”/>
Posted
Updated 17-Mar-17 12:45pm
v2

There are some mistakes in your pattern.

The - minus sign defines a range within a bracketed character class. So it has to be escaped or the first character if you want to match the character. When using +-\/ by intention then it matches all characters from 0x2b to 0x2f (which includes the dot which can then be removed).

The s at the end is useless.
[EDIT]
To allow white spaces it must be \s.
[/EDIT]

To check for valid lengths you have to match the whole string using ^ and $.

You have not specified which language you are using. Here is a working Perl regex:
PERL
# Edit: With white spaces
# With range from + to /
#if ($test =~ /^[A-Za-z0-9_@#&+-\/\s]{0,28}$/) {
if ($test =~ /^[A-Za-z0-9_@.#&+\-\/\s]{0,28}$/) {
    print "match";
}
else {
    print "no match";
}
 
Share this answer
 
v2
Comments
Member 13065591 17-Mar-17 9:34am    
I have used perl , as i am using "s" to allow space .... but i am using this as expression and use the file to validate the name .. it will fetch like ..
when i am testing, i found like it also passes the validation when i am giving name more then 28 char and truncate that and update the database with 28 char
Jochen Arndt 17-Mar-17 9:38am    
It was not quite clear for me if white spaces should be allowed or not.
But if they should be allowed you have to use \s.

I have tested it here:
No match if the string is longer (when using ^[...]{0,28}$).
Member 13065591 17-Mar-17 9:56am    
ya white space is used ... the entered value shows all those charc which are allowed.. only issue i am facing is ... if i enter any name more then 28 char ... rather rejecting it passes the value and truncate that and update upto 28 char.
Jochen Arndt 17-Mar-17 10:00am    
The above (updated for white spaces) code snippet is working as expected and prints "no match" if the string is too long and contains only allowed characters.
As you have been advised, then you have to include beginning and end of sting markers.

Just a few links in hope ir help you to build and test 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[^]
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