Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Regex: first match that is not enclosed in parenthesis

by james2vegas (Chaplain)
on Jun 30, 2013 at 10:17 UTC ( #1041566=note: print w/replies, xml ) Need Help??

in reply to Regex: first match that is not enclosed in parenthesis

You might want to split the string using Text::Balanced's extract_multiple function and then apply a map to extract your data (this code extracts the first '+' and replaces it with a '-'):
use strict; use warnings; use List::Util qw/first/; use Text::Balanced qw/extract_multiple extract_bracketed/; my $text = "1*(2+3)*(3+4)+5*(6+7)+42"; my @results = extract_multiple( $text, [ { Bracketed => sub { extract_bracketed( $_[0], '()' ) } }, { PlusOperator => qr{[+]} }, { MultiplyOperator => qr{[*]} }, { Number => qr{\d+(?:\.\d+)?} }, ] ); my $operator = first { ref($_) eq 'PlusOperator' } @results; # $operator holds '+' my $plus; my $new_string = join( '', map { if ( !$plus && ref $_ eq 'PlusOperator' ) { $plus = 1; '-' } else { $$_ } } @results ); print "$new_string\n";
or the answer is 'no', regexes that parse data like that are never going to be elegant.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1041566]
[Corion]: Viewing Tk Colours (Colors) by name has the list of all Tk colour names
[choroba]: Aren't they the standard X colours?
[ambrus]: choroba: that's not clear, since Tk runs on systems that don't even have X.

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2017-10-17 11:57 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (227 votes). Check out past polls.