Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Perl Programming Language

regular expression ignore example?


Hello,

I have looked all over trying to find an example of how to grab
certain parts of a text file while ignoring others in the same line.

the file:

*************test.txt*************************
CREATED AT: Wed-June6-17:50 2007
NUM OBS: 1440
Values of two numbers:30.0/45.0 More text
that can be disregarded.
***********************************************

Here is what I want to grab from it:

**************output************************
CREATED AT: Wed-June6-17:50 2007
VALUE: 30.0/45.0
**********************************************

Of course, the file is much larger than this, but I just want the
general syntax of how to accomplish this.

I've just started to use reg ex's, so bear with me...here is what I
have thusfar:

while(<file>){
if ($_=~ /(\d+)/)
{
print $_;

}
}

Thanks for your help!
Jonathan
On Jun 6, 2:15 pm, vorticitywo@gmail.com wrote:

A regular expression has two intentions.  First, it determines whether
or not a given string of text "matches" some pattern.  Second, it is
used to "pull" certain parts of that match out to be stored
elsewhere.  You are only using the first intent - you're determining
whether or not the line matches one or more digits.

What you should instead do is find out if this text matches the actual
format of the line, and secondarily pull out from that match the data
you want to keep:

#!/usr/bin/perl
use strict;
use warnings;

while (my $line = <DATA>) {
   if ($line =~ /^CREATED AT:/) {
      #If line starts with that text, print the entire line
      print $line;
   }
   if ($line =~ /^Values of.*?(\d+\.\d+\/\d+\.\d+)/) {
      #If line starts with Values of, pull out
      #the relevant text that you want to print
      print "VALUE: $1\n";
   }

}

__DATA__
CREATED AT: Wed-June6-17:50 2007
NUM OBS: 1440
Values of two numbers:30.0/45.0 More text
that can be disregarded.

The above program outputs:
CREATED AT: Wed-June6-17:50 2007
VALUE: 30.0/45.0

For more information on using meta characters and quantifiers (ie,
the .*? above) and the captured submatches (the $1), please have a
read of:
perldoc perlretut
perldoc perlre
perldoc perlreref

Paul Lalli

On Jun 6, 6:42 pm, Paul Lalli <mri@gmail.com> wrote:

Excellent! That is just the information that I need to get me going.
I thought there may be a way to  first pull in all the lines with
numbers and then clean them up with some code, and that is pretty much
what yours does! Thanks!

Jonathan

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc