ozboomer has asked for the wisdom of the Perl Monks concerning the following question:
I have a text record and I would like to extract the 2nd field from it, viz:
SLOT3=4,4,2!INT=115!VC=4!CS=270!PK=/
Now, the simple but kindof-obscure way to do it might be:
$rec = "SLOT3=4,4,2!INT=115!VC=4!CS=270!PK=/"; ($int_spec) = (split("!", $rec))[1]; $int = $int_spec; $int =~ s/INT=//; printf("\$int: $int\n");
The somewhat better way, although more obscure for people not used to regex might be:
$rec = "SLOT3=4,4,2!INT=115!VC=4!CS=270!PK=/"; $int = ($rec =~ /(INT=([0-9]*)!)/)[1]; printf("\$int: $int\n");
Perhaps there's an even more elegant way to do it using regex?
At any rate, I generally go for the simplest way of doing something, as the PCs we use these days are getting pretty quick (yes, Win32 development again)... but I want to get some more practice with using regex.
I looked in the on-line docs, amongst the monks articles and in the O'Reilly regex and cookbook books but couldn't find something simple... So I thought I'd drop a question in here.
Something else... In terms of maintenance, what is the 'best'(!?) way to go? Assume my maintainer is an expert in regex or go for the "lowest common denominator" (assuming performance isn't critical and simplicity is king)?
Would appreciate any thoughts....
|
|---|