#! c:\Perl\bin\perl5_8 #Read lines from a text file; create RE's that will find certain stuff #use strict; take this out for now open (MYFILE, "< Lori.txt") || die("Can't open Lori.txt"); #find lines starting with a c and end with a d print "lines starting with a c and end with a d:\n"; while (chomp($line =<MYFILE>)) { if ($line =~ /^[cC].*d$/o) { print "$line\n"; } } #find blank lines seek MYFILE, 0,0; print "\nblank lines:\n"; while (chomp($line =<MYFILE>)) { if ($line =~ /^$/o) { print "$line\n"; print "blank line above\n"; } } #find lines with only spaces seek MYFILE, 0,0; print "\nlines with only spaces:\n"; while (chomp($line =<MYFILE>)) { if ($line =~ /^ *$/o) { print "$line\n"; print "spaced out line above\n"; } } #find lines that contain the same number twice seek MYFILE, 0,0; print "\nlines that contain the same number twice:\n"; while (chomp($line =<MYFILE>)) { if ($line =~ /(\d+)[^\d]*\1[^\d]/o) { print "$line\n"; } } #find lines that contain a negative number with a decimal point seek MYFILE, 0,0; print "\nlines that contain a - number with a decimal point:\n"; while (chomp($line =<MYFILE>)) { if ($line =~ /-\d*\.\d*/o) { print "$line\n"; } } close MYFILE;
And my test text file Lori.txt:
Catdog isn't a horsebird
The next line is a blank line.
The next line is a line full of spaces.
fred1 fred2 fred3 fred3 fred2
lucy1 lucy2 lucy3 lucy4 lucy5
negative number with a decimal: -15.00
negative number with a decimal: -.15
positive number with a decimal: .15
negative number with a decimal: -7.
negative sign with a decimal: -.
Thanks for any insights you can provide. Please feel free to suggest ways of improving my code.    :-)
Lori
In reply to Regex negative number question by Lori713
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |