Mongers,

I want to take data from two different inputs and then I want to apply a common regex to extract hex digits from it; the problem is that one device spits the hex values as 0x and the other data doesn't use the 0x, which makes my regex looks a bit ugly

My idea is to have something like this:

# Both connections are telnet based sub getDataFromDevice { # login to the device # define the read/write commands &dumpData($conn, \%cmds) } sub getDataFromJtag { # login to the device # define the read/write commands &dumpData($conn, \%cmds) } sub dumpData { my $conn = shift; my $cmds = shift; my $addr = qr/^\s*0?x?[[:xdigit:]]{8}:\s+0?x?([[:xdigit:]]{8}).*$/ +; @output = $conn->cmd("$cmd->{read} 0xfeedbeef"); push @obuf, (grep {s/$addr/$1/} @out); }

This is what the two different commands output looks like

% read 0xfeedbeef f80e205c: 30000000 ........ 805306368

ppc8641-A[0,h] % mr 0xfeedbeef 0xf80e2044: 0x00100000 ........

The main issue I have is the 0?x? format in the $addr scalar looks horrible, so I was thinking to use grouping like (0x)? but the problem is this will break the substitution so I am wondering if there another way to write (0x)? without storing the data in $1

Thanks for your wisdom


In reply to Regex grouping without storing by gmoque

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.