regular expression for digits
regular expression for digits
am 15.11.2007 10:36:34 von Abanowicz Tomasz
Hello
My @files array contains file names:
11.jpg
111.jpg
0121.pic
1.gif
1
pic 1 cool.gif
aaa1bbb.jpg
I would like to "grep" files containing "1" digit and not "11", "111",
"0121" etc.:
1.gif
1
pic 1 cool.gif
aaa1bbb.jpg
I use the following to do that:
grep (/[^0-9]+$i[^0-9]+/, @files)
Unfortunately it does not match:
1.gif
1
files.
How should my grep look in order to cover those 2 files as well.
thank You for help
Re: regular expression for digits
am 15.11.2007 10:45:53 von Yann.Esposito
use * and not +
Y.
Abanowicz Tomasz a =E9crit :
> Hello
> My @files array contains file names:
> 11.jpg
> 111.jpg
> 0121.pic
> 1.gif
> 1
> pic 1 cool.gif
> aaa1bbb.jpg
>
> I would like to "grep" files containing "1" digit and not "11", "111",
> "0121" etc.:
> 1.gif
> 1
> pic 1 cool.gif
> aaa1bbb.jpg
>
> I use the following to do that:
>
> grep (/[^0-9]+$i[^0-9]+/, @files)
>
> Unfortunately it does not match:
> 1.gif
> 1
>
> files.
>
> How should my grep look in order to cover those 2 files as well.
>
> thank You for help
Re: regular expression for digits
am 15.11.2007 10:50:13 von Peter Makholm
Abanowicz Tomasz writes:
> I would like to "grep" files containing "1" digit and not "11", "111",
[...]
> I use the following to do that:
>
> grep (/[^0-9]+$i[^0-9]+/, @files)
What is $i and is it relevant?
If you just need to find stings containing one and only one digit I
would use tr///:
grep { tr/0-9// == 1 } @files;
//Makholm
Re: regular expression for digits
am 15.11.2007 12:55:44 von Paul Lalli
On Nov 15, 4:36 am, Abanowicz Tomasz wrote:
> Hello
> My @files array contains file names:
> 11.jpg
> 111.jpg
> 0121.pic
> 1.gif
> 1
> pic 1 cool.gif
> aaa1bbb.jpg
>
> I would like to "grep" files containing "1" digit and not "11", "111",
> "0121" etc.:
> 1.gif
> 1
> pic 1 cool.gif
> aaa1bbb.jpg
>
> I use the following to do that:
>
> grep (/[^0-9]+$i[^0-9]+/, @files)
>
> Unfortunately it does not match:
> 1.gif
> 1
>
> files.
>
> How should my grep look in order to cover those 2 files as well.
You need to look for a 1 that's preceded by either a non-digit or the
start of string, and followed by either a non-digit or the end of
string:
/(?:^|\D)1(?:\D|$)/
Paul Lalli
Re: regular expression for digits
am 15.11.2007 13:01:39 von Paul Lalli
On Nov 15, 4:45 am, Yann.Espos...@gmail.com wrote:
> Abanowicz Tomasz a =E9crit :
> > My @files array contains file names:
> > 11.jpg
> > 111.jpg
> > 0121.pic
> > 1.gif
> > 1
> > pic 1 cool.gif
> > aaa1bbb.jpg
>
> > I would like to "grep" files containing "1" digit and
> > not "11", "111",
> > "0121" etc.:
> > 1.gif
> > 1
> > pic 1 cool.gif
> > aaa1bbb.jpg
>
> > I use the following to do that:
>
> > grep (/[^0-9]+$i[^0-9]+/, @files)
>
> > Unfortunately it does not match:
> > 1.gif
> > 1
>
> > files.
>
> > How should my grep look in order to cover those 2 files as well.
> use * and not +
Please don't guess when you don't know the answer. This is blatantly
incorrect. grep (/[^0-9]*$i[^0-9]*/, @files) allows 0 nondigits
immediately before or after, but doesn't say anything about what comes
before or after the 0 nondigits.
Paul Lalli
Re: regular expression for digits
am 16.11.2007 06:14:16 von Petr Vileta
Abanowicz Tomasz wrote:
> Hello
> My @files array contains file names:
> 11.jpg
> 111.jpg
> 0121.pic
> 1.gif
> 1
> pic 1 cool.gif
> aaa1bbb.jpg
>
> I would like to "grep" files containing "1" digit and not "11", "111",
> "0121" etc.:
> 1.gif
> 1
> pic 1 cool.gif
> aaa1bbb.jpg
>
Maybe this can help you (image name I assume in $var)
if(($var =~ s/1/1/g) == 1) { do something }
or this
if($var =~m/^[^1]*1[^1]*\.\D+) { do something }
--
Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
Re: regular expression for digits
am 19.11.2007 14:09:13 von rvtol+news
Abanowicz Tomasz schreef:
> My @files array contains file names:
> 11.jpg
> 111.jpg
> 0121.pic
> 1.gif
> 1
> pic 1 cool.gif
> aaa1bbb.jpg
>
> I would like to "grep" files containing "1" digit
perl -wle '
my @files = qw/11.jpg 111.jpg 0121.pic 1.gif 1 pic_1_cool.gif
aaa1bbb.jpg/;
print for grep /^[^0-9]*[0-9][^0-9]*$/, @files;
'
1.gif
1
pic_1_cool.gif
aaa1bbb.jpg
Alternative: grep 1 == (() = /[0-9]/g), @files;
--
Affijn, Ruud
"Gewoon is een tijger."