I finally had my first Perl class (three days of wonderful bliss. Yay!). Anyways, we were working on creating regexes to find different things, and the last regex block in my code (finding a negative number with a decimal) stumped everyone (even the teacher at the time). We wanted the regex to find all but the last line in my text file. My last regex finds the last six lines of my text file, but I don't want it to find the last line. Assuming that the "abc" in that last line could be anything, how do I exclude it if it doesn't follow the same pattern as the examples above it and not eliminate stuff I do want?
#! 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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.