match three digit number using regular expression

  • Thread starter championsleeper
  • Start date
C

championsleeper

i am trying to write a script that will:
- check an integer value to see if it has a particular pattern
(regular expression match)
- tell me what the particular pattern is.

I am searching integer numbers for three digit patterns of the form
111, 222, ..., 999, 000. If the number contains one or more of these
then I want the script to say the pattern I wanty has been matched and
which pattern it has found.

e.g
252352524577778787 -> yes! 777
18357000232333 -> yes! 000

I have written a ksh script to do this but want to write something in
awk or perl that can do the same (better portability). I've been
reading up on regular expressions but to no avail. Ideally I'd like to
do the matching without recourse to a loop which is what I currently
have.

Any ideas?
 
E

Ed Morton

championsleeper said:
i am trying to write a script that will:
- check an integer value to see if it has a particular pattern
(regular expression match)
- tell me what the particular pattern is.

I am searching integer numbers for three digit patterns of the form
111, 222, ..., 999, 000. If the number contains one or more of these
then I want the script to say the pattern I wanty has been matched and
which pattern it has found.

e.g
252352524577778787 -> yes! 777
18357000232333 -> yes! 000

I have written a ksh script to do this but want to write something in
awk or perl that can do the same (better portability). I've been
reading up on regular expressions but to no avail. Ideally I'd like to
do the matching without recourse to a loop which is what I currently
have.

Any ideas?

The trivial solution is:

awk '/000/ { print "yes! 000"; next }
/111/ { print "yes! 111"; next }
....
/999/ { print "yes! 999"; next }'

If your pattern contains both "111" and "333" and you want both
reported, just remove the "; next" from each line.

Ed.
 
D

Daboo

@comp=('111','222','333','444','555','666','777','888','999','000');

$num=252352524577778787;
#$num=18357000232333;

foreach (@comp)
{
$numfind=$_;
$foo = grep(!/$numfind/, $num);
print "$numfind: $foo\n";
}

HTH,

Daboo
 
G

Gunnar Hjalmarsson

Daboo said:
@comp=('111','222','333','444','555','666','777','888','999','000');

$num=252352524577778787;

You'd better make it a string.

$num = '252352524577778787';
#$num=18357000232333;

foreach (@comp)
{
$numfind=$_;
$foo = grep(!/$numfind/, $num);

Why are you using grep() to match one scalar? If you are trying to
find the number of occurrences of $numfind in $num, that's not the way
to do it. This is one way:

$foo = () = $num =~ /$numfind/g;
print "$numfind: $foo\n";
}

This is an alternative, shorter Perl solution:

$_ = '252352524577778787';
print 'yes! ', $1 x 3, "\n" while /(\d)\1\1/g;
 
B

Berk Birand

The trivial solution is:

awk '/000/ { print "yes! 000"; next }
/111/ { print "yes! 111"; next }
....
/999/ { print "yes! 999"; next }'

If your pattern contains both "111" and "333" and you want both
reported, just remove the "; next" from each line.

You really don't have to do that. In my opinion this is not even
considered programming, since it basically lists all possible choices.

Here's a simple script thing I wrote that does what you want.

#!/usr/bin/perl

print "Gimme a number";

$input = <STDIN>;

if ($input =~ /.*(\d)\1\1.*/ ) { #this is the line that does it all
print "found line", $1*111;
} else {
print "found nothing";

}

Basically the "if" line does everything. \d stands for digit. Since I put
it in the brackets, I can refer to the number found later with \1.
Translated to english, that would be:

"Find a string starting with something, which includes a digit, followed
by two instances of the same digit and finished by something else."

We can then read the single digit value in perl using the special variable
$1, which contains the result of the first parenthesis of the RegExp.
Multiplying by 111 is simply a little math trick that will output the same
digit thrice ( I couldn't find an operator that repeats a string ).

Also, if you have multiple three consecutive digits, this will only math
the *last* one. 8791118797666778 will return 666 only, and will discard
the 111. Some more modifications are needed, and I am too lazy to do them
now. If that is what you need, let me know.

Cheers,
BB
 
E

Ed Morton

Berk said:
You really don't have to do that. In my opinion this is not even
considered programming, since it basically lists all possible choices.

It's arguably the best form of programming when providing code for
beginners who haven't precisely specified their requirements and
therefore may need to modify it themselves - KISS.
Here's a simple script thing I wrote that does what you want.

Maybe.

Also, if you have multiple three consecutive digits, this will only math
the *last* one. 8791118797666778 will return 666 only, and will discard
the 111. Some more modifications are needed, and I am too lazy to do them
now. If that is what you need, let me know.

Precisely. What I provided was something obvious that the OP could
easily tweak based on their detailed requirements or enhance in future,
plus the instructions on how to modify it. What you provided was
something that, while it would concisely satisfy one interpretation of
the requirements, would need non-trivial rework (I'm assuming that's the
case or you'd have done it) to satisfy a different interpretation. Both
solutions have some merit based on what the OP wants to get out of this
exercise.

Ed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top