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

Hi, I have q query like this:((A AND B) OR (C AND D) NOT E)i have to process this query and match for these words in file and display the matched results. How do i extract the words which is in paranthesis(i.e A AND B,C AND D)? If many brackets are there how do i extract? With regards Archana

Replies are listed 'Best First'.
Re: Extract words from brackets
by atemon (Chaplain) on Jul 19, 2007 at 10:20 UTC

    Hi,

    If extraction of contents in inthe inner paranthesis is your only concern, try

    $str = "((A AND B) OR (C AND D) NOT E)"; print "$1\n" while( $str =~ /\(([\w\s]+)\)/g);

    this may not be the best solution, but this is the first solution I have.

    Cheers !

    --VC


    There are three sides to any argument.....
    your side, my side and the right side.

      Hi, This works for multiple brackets that is (((A AND B) OR (C AND D) NOT (E AND F)))? with regards Archana
Re: Extract words from brackets
by GrandFather (Saint) on Jul 19, 2007 at 10:16 UTC

    Perhaps you need to take two steps back and describe the larger picture? It seems likely that you are trying to parse an expression. If that is the case then parsing out the words is the smallest part of the problem.


    DWIM is Perl's answer to Gödel
      Hi, Yes i am parsing the expression but if many brackets are there say for eg:-(((A AND B) AND (C OR D)NOT (E AND F))) Then how do i proceed to extract the contents and search for the words?
Re: Extract words from brackets
by daxim (Curate) on Jul 21, 2007 at 07:34 UTC