Helpful Information
 
 
Category: Regex Programming
Find exact string length and remove

Hi, how do I validate a string and remove it if it doesn't match the desired length? For example if a phone number is too short or too long, remove it, but if it is the desired length of 11 characters, leave as is?

Thanks in advance

Andy

You don't need a regular expression for that. Your programming language will have some way of finding the length of a string (e.g. Java's String class has a length() method, Perl has a 'length' function, etc.).

You're right Ishnid, I can use my language to check the length, however, I have a group of (pre-built) regexs to remove non-alpha characters, remove spaces etc and I'll like to add to the functionality and make it invisible to my users.

After you remove the invalid characters, check the length.

Be warned though that if someone types "123 14th street, new york new york 10036" and you strip out the non-numeric characters, the result will APPEAR to be a phone number, but won't be.

-Dan










privacy (GDPR)