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

{TAG} 0012310002 and 0012310003 {COUNT} 000000 {COUNT2} 000000 and 100001
how to write a regular expression, to find the text between closing of one '}' to start of another '{'. And also check if 'and' is present, if 'and' is present, then I have to print "{TAG} and {COUNT2} is 1".

Replies are listed 'Best First'.
Re: Check for 'and'
by Anonymous Monk on Aug 11, 2009 at 08:53 UTC
    1. You should write a parser (or find an existing one, its easy if you know your format's name).
    2. { [^}]+ }
      How to store the values to a variable, {TAG} to { start of another braces.
        {TAG} 0012310002 and 0012310003 {COUNT} Please tell me how to write regular expression to store value after {TAG} and start of '{' of COUNT.
Re: Check for 'and'
by Bloodnok (Vicar) on Aug 11, 2009 at 12:35 UTC
    It seems, to me, given the above structure, that since the data i spread over multiple lines. the solution lies in using a flip-flop operator e.g.
    use warnings; use strict; my $save; while (<DATA>) { if (/^\{TAG\}/ .. /^\{COUNT\}/) { unless (/^\{(TAG|COUNT)\}/) { $save = $_; print "{TAG} and {COUNT2} is 1\n" if /\sand\s/; } } } __DATA__ {TAG} 0012310002 and 0012310003 {COUNT} 000000 {COUNT2} 000000 and 100001
    Produces (not entirely unexpectedly:-):
    $ perl tst.pl 0012310002 and 0012310003 $

    Update:

    Arrrgh!! Omitted 2nd question - now included. Also didn't show how to save the data between the given tags - also now included.

    A user level that continues to overstate my experience :-))
Re: Check for 'and'
by JavaFan (Canon) on Aug 11, 2009 at 09:43 UTC
    It seems you are asking two questions. For the first, I'd recommend /([^}]*)/. For the second:
    print "{TAG} and {COUNT2} is 1" if /and/;
Re: Check for 'and'
by youlose (Scribe) on Aug 11, 2009 at 16:31 UTC
    Maybe you seeking something like this:


    #!/usr/bin/perl -w
    use strict;
    use 5.010;
    my $str=<<TEXT;
    {TAG}
    0012310002 and 0012310003
    {COUNT}
    000000
    {COUNT2}
    000000 and 100001
    TEXT
    my %strh = $str=~ /({.+?}|^{}+)/sg;
    my @and_contain = grep $strh{$_} =~ /and/,keys %strh; 
    say join(' and ',@and_contain),' is 1';

    Output:
    
    {COUNT2} and {TAG} is 1
    
      #!/usr/bin/perl use warnings; use strict; my $save; while (<DATA>) { if (/([^{TAG}]*)/) { $save = $1; print $save; } } __DATA__ {TAG} 0012310002 and 0012310003 {COUNT} 000000 {COUNT2} 000000 and 100001 {TAG} 2304854
      How to print the value of only {TAG}. Now the output prints all the values like, 0012310002 and 0012310003
      000000
      000000 and 100001
      2304854
      So how to print only 0012310002 and 0012310003
      and 2304854
      which of {TAG} values