regular expression for digits

A

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
 
P

Peter Makholm

Abanowicz Tomasz said:
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
 
P

Paul Lalli

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
 
P

Paul Lalli

Abanowicz Tomasz a écrit :
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

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
 
D

Dr.Ruud

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;
 

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,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top