Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Regex grouping without storing

by gmoque (Acolyte)
on Oct 06, 2011 at 17:11 UTC ( #930023=perlquestion: print w/ replies, xml ) Need Help??
gmoque has asked for the wisdom of the Perl Monks concerning the following question:

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

Comment on Regex grouping without storing
Select or Download Code
Re: Regex grouping without storing
by AnomalousMonk (Abbot) on Oct 06, 2011 at 17:31 UTC
Re: Regex grouping without storing
by AR (Friar) on Oct 06, 2011 at 17:31 UTC
    (?:0x)?
Re: Regex grouping without storing
by davido (Archbishop) on Oct 06, 2011 at 17:54 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://930023]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-12-25 02:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (159 votes), past polls