Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: bracket processing

by 1nickt (Canon)
on Mar 31, 2020 at 02:04 UTC ( [id://11114821]=note: print w/replies, xml ) Need Help??


in reply to bracket processing

Hi,

Just remove the matching string (and whatever's left over) when you find that the RE matches. The matched string will be in $1 as normal and the original string will be trimmed to just the prefix.

use strict; use warnings; use 5.010; use Regexp::Common 'RE_balanced'; my $string = 'The use of parentheses (indicates that the ' . '(writer [considered] the {information}) less ' . '<important—almost> an afterthought).'; my $match = RE_balanced( -parens => '(){}[]<>' ); $string =~ s/${match}.*//; $1 and say ">$_<" for $string, $1;
Output:
$ perl 11114819.pl >The use of parentheses < >(indicates that the (writer [considered] the {information}) less <imp +ortant—almost> an afterthought)<

Hope this helps!


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: bracket processing (updated)
by AnomalousMonk (Archbishop) on Mar 31, 2020 at 05:48 UTC

    Update: Per haukex's query, the version of Regexp::Common::balanced I'm using is 2010010201, so yes, I'm a bit behind the times and the behavior of 1nickt's code is not unexpected.

    c:\@Work\Perl\monks>perl -wMstrict -le "use Regexp::Common qw(balanced); print $Regexp::Common::balanced::VERSION; " 2010010201


    Are you sure about that code? I can only generate the given output when I add the  -keep switch to the  RE_balanced() call.

    Without -keep:

    c:\@Work\Perl\monks>perl -wMstrict -le "use strict; use warnings; use 5.010; use Regexp::Common 'RE_balanced'; my $string = 'The use of parentheses (indicates that the ' . '(writer [considered] the {information}) less ' . '<importantalmost> an afterthought).'; my $match = RE_balanced( -parens => '(){}[]<>' ); $string =~ s/${match}.*//; $1 and say \"^>$_^<\" for $string, $1; "
    With -keep:
    c:\@Work\Perl\monks>perl -wMstrict -le "use strict; use warnings; use 5.010; use Regexp::Common 'RE_balanced'; my $string = 'The use of parentheses (indicates that the ' . '(writer [considered] the {information}) less ' . '<importantalmost> an afterthought).'; my $match = RE_balanced( -parens => '(){}[]<>', -keep ); $string =~ s/${match}.*//; $1 and say \"^>$_^<\" for $string, $1; " >The use of parentheses < >(indicates that the (writer [considered] the {information}) less <imp +ortantalmost> an afterthought)<


    Give a man a fish:  <%-{-{-{-<

      I can only generate the given output when I add the -keep switch to the RE_balanced() call.

      What version are you using? From Regexp::Common::balanced:

      Since version 2013030901, $1 will always be set (to the entire matched substring), regardless whether {-keep} is used or not.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-24 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found