Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Regex grouping without storing

by gmoque (Acolyte)
on Oct 06, 2011 at 17:11 UTC ( [id://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

Replies are listed 'Best First'.
Re: Regex grouping without storing
by davido (Cardinal) on Oct 06, 2011 at 17:54 UTC
Re: Regex grouping without storing
by AnomalousMonk (Archbishop) 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)?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-25 12:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found