ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Use the character class grouping [] combined with the ^ (meaning characters not in this class).
# check #1 - phone number should be at least digits, then a dash, then
# at least 5 digits
# check #2 - first section cannot be all 0's
# check #3 - second section cannot be all 0's
# Quick reg exp refresh
# \d = digit
# \d{n,} = at least n digits
# ^ - start of string
# \ escape a metacharacter (like a dash) to mean its literal value
# $ - end of string
# [] character class (match anything inside of the brackets
# [^] character class negated (match on anything NOT inside the brackets
### if $phone_number is the number you want to check
if (
$phone_number =~ /^\d{2,}\-\d{5,}$/ ### check #1
and $phone_number =~ /^[^0]{2,}\-/ ### check #2
and $phone_number =~ /\-[^0]{5,}$/ ### check #3
) {
print "GOOD: $phone_number\n";
} else {
print "BAD: $phone_number\n";
}
You could also use the !~ operator to do check 2 and 3
e.g. $phone_number !~ /^0+\-/
or
$phone_number !~ /^0{2,}-/
Well that is curious, because I copy/pasted that code into a script and whenever
either part of the number has a zero in it I am told the number is BAD??
So I would be interested to know if you changed any of it as our results are very different?
Lots of reasons a regular expression match can fail. Off the top of my head, I'd guess you have white space somewhere in your string. Try putting single quotes or some other character around the GOOD/BAD messages. For example:
print "BAD: '$phone_number'\n";
If $phone_number == '01-00000\n' or '01-00000 ' the third check will fail.
For a simple case like this one, print statements are usually enough to figure out what's going wrong. For more complex cases, you can break the 3 checks into individual checks and nest them (e.g. if (check 1) { if (check 2) { if (check 3) { ... which makes it easier to figure out exactly which check is failing.
Finally, I'd suggest learning about perl -d (the perl debugger). Just type perl -d <your perl program + options> and then type h at the prompt. This is an invaluable tool for debugging issues of any complexity.
also, all the code here is off the top of my head w/o any testing... so don't assume it just works. I think the idea is good and I tried to write it correctly, but "code that is not tested does not work correctly".
THe code is working fine for me now
For supporting more than 2 charcters in prefix and morethan 5 characters in postfix i added a comma in the first condition...
if ($phone_number =~ /^\d{2,}\-\d{5,}$/ and $phone_number !~ /^0{2}\-/ and $phone_number !~ /\-0{5}$/)
{
print "GOOD: $phone_number\n";
}
else
{
print "BAD: $phone_number\n";
}
I have tested this and working in all conditions...
Thanks alot for both of yours help and comments.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.