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.I'm sure that some regex wizard may come up with a single regex to do it. If nothing else, you can always put some code into the replacement part of s/// and you can do other matches or substitutions in it. Whatever, it would be clumsy and unncessary...
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.I really can't see why it should be "inconvenient": do you happen to work on some inner component of your car without unmounting it first? I suppose you do, instead, and then remount everything together when you're done, don't you? Here it's reasonable to break your string into chunks and operate on them, especially since
All in all I would do it like thus:
Note how simple and concise the effective code is.#!/usr/bin/perl -lpi use strict; use warnings; my @chunks=split /\|/; next unless $chunks[0] eq 'OBX'; s/(?=[A-Z])/U/g, s/(?=[a-z])/L/g for $chunks[2]; $_=join '|', @chunks; __END__
In reply to Re^3: Can anyone make this regex job neater?
by blazar
in thread Can anyone make this regex job neater?
by Melly
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |