A simple query, this one, I think... but of interest to those who aren't too flash on regex (like me!)

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....


In reply to Simple RegEx Substring Extraction from a Delimited Text Record by ozboomer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.