Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

extract info from parentheis

by xiaoyafeng (Deacon)
on Mar 21, 2015 at 05:45 UTC ( [id://1120826]=perlquestion: print w/replies, xml ) Need Help??

xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

I have a string contains all users and id like:
the users contain (bbc (333)) BLAH BLAH (ddc (223)) BLAH BLAH(ccc (123 +)).......
is there a faster, smart way to get all names and ID in parenthesis? Thanks in advance




I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: extract info from parentheses
by Athanasius (Archbishop) on Mar 21, 2015 at 06:19 UTC

    Hello xiaoyafeng,

    What’s wrong with a straightforward regex?

    #! perl use strict; use warnings; $_ = 'the users contain (bbc (333)) BLAH BLAH (ddc (223)) BLAH BLAH(cc +c (123))'; print "Name: $1, ID: $2\n" while / \( ([^(\s]+) \s* \( ([^)]+) \)\) /g +x;

    Output:

    16:17 >perl 1194_SoPW.pl Name: bbc, ID: 333 Name: ddc, ID: 223 Name: ccc, ID: 123 16:17 >

    Update: The module Text::Balanced has an extract_bracketed function which might be worth investigating.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      I often think Text::Balanced is going to be the answer, but after I implement a solution using Text::Balanced I'm rarely satisfied that it is the least complex solution:

      use strict; use warnings; use Text::Balanced 'extract_bracketed'; my $string = q/the users contain (bbc (333)) BLAH BLAH (ddc (223)) BLA +H BLAH(ccc (123))/; while( length $string ) { my( $name, $id, $rest ) = extract_record($string); print "Name: $name, ID: $id\n"; $string = $rest; } sub extract_record { my $string = shift; my( $unwanted, $wanted, $rest ) = extract($string); return (extract($wanted))[0,1], $rest; } sub extract { my $input = shift; my( $balanced, $rest, $prefix ) = extract_bracketed($input, '()', +qr/[^(]*/); $balanced =~ s/^\(|\)$//g if length $balanced; return map { trim($_) } $prefix, $balanced, $rest; } sub trim { my $string = shift; $string =~ s/^\s+|\s+$//g; return $string; }

      Regexp::Common has Regexp::Common::balanced which would probably facilitate a reasonably robust and possibly less complicated solution (left as an exercise for the reader, as I've lost interest at this point. ;)

      Update: But a pure-regexp approach would work for data that is relatively simple, as the OP's data seems to be:

      my $string = q/the users contain (bbc (333)) BLAH BLAH (ddc (223)) BLA +H BLAH(ccc (123))/; my $re = qr/ \( (?<name> [^(]+ ) # Capture name. \( (?<id> [^)]+ ) # Capture ID. \)\) # Closing parens. /x; print "Name: $+{name}, ID: $+{id}\n" while $string =~ m/$re/g;

      ...which is pretty much back to what you started with. ;)


      Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-03-28 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found