in reply to Re: Can anyone make this regex job neater?
in thread Can anyone make this regex job neater?
This smells a lot like homework, so I'm not posting a full answer -- hints only!
Heh - well, not really, since I already have a solution.
It was just that I was curious to see if such a task could be handled with a single regex, or whether my instinct to break the job into seperate regexes was correct.
Thanks to all for input - it looks like my original instinct was correct. Other solutions exist (of course - hey, it's perl), but split is inconvenient, since I'd need to rejoin later. Maybe it would be a little faster - I dunno - but (high) speed isn't really an issue with this particular job.
Once again, thanks to all, and if someone does come up a single regex, I'd still be curious to see it.
Here, btw, is the full code I'm using - if anyone notices anything dangerous or just plain silly, feel free to comment. Always happy to learn.
use strict; open(IN, $ARGV[0])||die "Cannot open $ARGV[0] for read:$!\n"; my @lines = <IN>; close IN||die "Cannot close $ARGV[0]:$!\n"; open(OUT, ">$ARGV[0]")||die "Cannot open $ARGV[0] for write:$!\n"; foreach(@lines){ if(m/(^OBX\|)([^\|]*\|){2}([^\^]*)(.*$)/){ my $pre = $1 . $2; my $read = $3; my $post = $4; $read =~ s/[A-Z]/U$&/g; $read =~ s/[a-z]/L$&/g; $_ = $pre . $read . $post; } print OUT||die "Cannot write to $ARGV[0]:$!\n"; } close OUT||die "Cannot close $ARGV[0]:$!\n";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Can anyone make this regex job neater?
by GrandFather (Saint) on Oct 11, 2005 at 19:56 UTC | |
|
Re^3: Can anyone make this regex job neater?
by blazar (Canon) on Oct 12, 2005 at 07:47 UTC |