Re: Reg. Expression problem
by jlistf (Monk) on Aug 02, 2000 at 20:22 UTC
|
use strict;
use warnings;
open IN, "file.txt" or die "can't open : $!\n";
while (<IN>)
{
# perform some tests on $_
# if its a good line, print it.
}
close (IN);
if you want anything more, you have to give us more details.
jeff
ps. use HTML formatting, <BR>, instead of carriage returns. | [reply] [d/l] |
|
I have a huge file that contains several lines. How do I use regular expression to match the one I want. let's say the file is like this
perlmonks 1
perlmonks(1) 2
perlmonks(perl) 3
'perlmonks (67) 4
..... ... and so on... Let's say I just want to print
1
3
4 (skiping that perlmonks part
I just wanted to print the numbers I want on the right side. Thankx
| [reply] |
|
while (<FILE>)
{
print "$1\n" if $_ =~ m/(\d+)$/
}
this will print the last number on each line, if it is the last character on the line. for any more help, you're going to have to let us know how you decide that you want to print something. | [reply] [d/l] |
|
|
Re: Reg. Expression problem
by nardo (Friar) on Aug 02, 2000 at 20:17 UTC
|
Do you want to print 1 3 4 or 1 2 3 4? If you just want to get the last number(s) on a line, you could
print "$1 " if(/(\d+)$/);
If in fact you do not want to print the 2, I don't know what it is about that line which makes you want to skip it so you'll need to explain your problem in more detail. | [reply] [d/l] |
Re: Reg. Expression problem
by Boogman (Scribe) on Aug 02, 2000 at 20:25 UTC
|
If the line numbers are always at the start of the line and you
just wanted to pull out certain ones, you could say
while ( <FILE> ) {
print if ( /^(1|3|4)/ );
}
If the line is always a number followed by "perlmonks(stuff)"
and you just want to grab the stuff, you could say
while ( <FILE> ) {
if ( /^(1|3|4) perlmonks\(([^\)])\)/ ) {
print "$2\n";
}
}
Is that the checking you wanted to do? If not, you need to
describe what you want to be doing a bit better. Anyways,
hope that helps. | [reply] [d/l] [select] |
Re: Reg. Expression problem
by Anonymous Monk on Aug 02, 2000 at 20:53 UTC
|
Thankx jeff, If I want, suppose, 32 different numbers. 1, 34 , 45, 62...and other random ones
Do I have to write all the numbers or is there any other easier way?.......
| [reply] |
|
well, unless you have successfully compiled ESP.pm, you'll have to list the numbers in the regex :-)
BlueLines
Disclaimer: This post may contain inaccurate information, be habit forming, cause atomic warfare between peaceful countries, speed up male pattern baldness, interfere with your cable reception, exile you from certain third world countries, ruin your marriage, and generally spoil your day. No batteries included, no strings attached, your mileage may vary.
| [reply] |