Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regex Question - challenging 2

Status
Not open for further replies.

bra1niac

Programmer
Jun 13, 2001
127
US
Ok, I've been working with this for about a week (off and on, about 8 hours work) and I can't seem to get the results I want. I'm a fairly basic regex user. I have never really had a need for really complex search patterns like this one.

I have a collection of files that are all CSV. There is one field I need to check in all of them. The field is between the 45th & 46th set of double quotes. The value I'm looking for is a date represented like [0-9]+-[A-Z]+-[0-9]+

The hard part is that I can't seem to get any results back no matter how I try to tell my expression to search ONLY that field (see above). I can get it to return all dates out of the file in that format (10-SEP-04) but I can't constrain it to look only in a particular field.

I would have thought that it would be a valid pattern to look in one set place on each line, but I'm coming up short on the syntax. Can anyone help?

"It's easier to ask forgiveness than it is to get permission." - Rear Admiral Dr. Grace Hopper
 
my $line = ',blah,blah,10-SEP-04,blah,10-AUG-05';
my @r = $line=~/([0-9]+-[A-Z]+-[0-9]+)/g;
print join(",", @r);
 
Maybe it would be easier to use Text:CSV_XS to read the files into an array a line at a time. This will handle all the quirks and problems normally associated with parsing CSV files. Then just examine the requisite array item to see if it's what you want?
 
Well, the problem is that the string will contain any number of dates in the same format. The only way I could find the one I wanted was to think as if it were positional.

A little more detail: I was trying to write a script that would parse through any number of these CSV files (could be 500 or more), look for that field and return the row of data back ONLY if that field contained a date. I would then group those rows by the file, provide a summary count and be done with the whole mess.

Does that make sense?

"It's easier to ask forgiveness than it is to get permission." - Rear Admiral Dr. Grace Hopper
 
Yes, you can extract the value of the field by using Text::CSV (or, as bra1niac recommended, Text::CSV_XS, which I haven't used). Search on for those modules. Once you've done that, it's easy to check if it contains a date.
 
Hi,

Yeah, Text::CSV is a LOT easier :)

Code:
     local $CSV::Delimiters = q{","};
     my @cut = CSVsplit($data);

Then, just call the it with;

$cut[44] for the 45th field
$cut[45] for the 46th field
..etc

Hope that helps :)

Cheers

Andy
 
Using Text::CSV - I never would have thought of that!

Starless Steve
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top