in reply to Parsing Strings

Not stupid -- except (some would argue) for the failure to use strict, warnings and some prints -- and clumsy (IMO) only in your indentation.

That said, the following seems to be what you want. More commentary follows:

#!/usr/bin/perl -w use strict; use 5.018; # 1157516 my $sp = ".A(~(B & C & ( D | (E & F) ))),"; # $pin = string between . and ( if ( $sp =~ /\.(.*?)\(/ ) { my $pin = $1; say "|-- $pin --|\n"; } my $sig; # if ( $sp =~ /\((.*?)\)/ ) { if ( $sp =~ /\( (.*?\){2}) .*/x ) { $sig = $1; } say "\t|-> $sig <-|";

The /x modifer isn't necessary; it merely makes it easier to read the second regex; the use of the quantifier lets the regex capture all but the last ")" (the space between the one which trails "F" before the 3 parens leaves the one after the "F" out of consideration).

Replies are listed 'Best First'.
Re^2: Parsing Strings
by doofus (Acolyte) on Mar 14, 2016 at 18:59 UTC
    Thank you all for the suggestions. Hi Ww, Adding an extra (), then ending ) is missing. Any ideas?
    Test 1 : sp = .A(~(B & C & ( D | (E & F) ))), pin = A sig = ~(B & C & ( D | (E & F) )) Test 2 : sp = .A(~((B & C & ( D | (E & F) )))), pin = A sig = ~((B & C & ( D | (E & F) ))

      Now you've asked a question that begs for a reply of "lazy question." But, I suggest you direct your attention to the regex docs, with special attention to quantifiers (of the sort I illustrated.

      At your command prompt: perldoc perlre<c>, <c>perldoc perlrequick and perldoc perlretut and similarly for other questions about perl (perldoc perldoc is a good place to start).


      Questions containing the words "doesn't work" (or their moral equivalent) will usually get a downvote from me unless accompanied by:
      1. code
      2. verbatim error and/or warning messages
      3. a coherent explanation of what "doesn't work actually means.
        Thank you for your honesty and I do appreciate your help. I figured out \((.*?)\){3} would make it work catching the 3rd (), but it'd drop anything with less than 3 (). The script simply needs to pick up the contain in the outermost () without knowing how many () are there.
        .A(B) .A((B)) .A(((B))) So, \((.*?)\){2} doesn't work for me tried \((.*?)\)+ but not working either....
        there must be a simpler way..... Anyone?