This is PerlMonks "Mobile"

Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  


in reply to Matching a string in a parenthesized block (regex help)

$ echo "ASDF { tmp plz_match tmp } string2 { tmp } string3 { tmp } " | perl -e' local $/ = "}\n"; while ( <> ) { if ( /^ASDF/ && /plz_match/ ) { print "Matched: $_"; ++$match; } } print "No match\n" unless $match; ' Matched: ASDF { tmp plz_match tmp }

Replies are listed 'Best First'.
Re^2: Matching a string in a parenthesized block (regex help)
by maxamillionk (Acolyte) on Mar 06, 2021 at 01:51 UTC

    I tried implementing your solution here:

    use warnings; use strict; my $file = "/path/to/file.txt"; sub has_word { my $arg = $_[0]; local $/; open FILE, '<', $file; while ( <FILE> ) { if ( /^ASDF_$arg/ && /magic/ ) { close FILE; return 1; } else { close FILE; return 0; } } } sub main { if (has_word("ONE")) { print "ONE already has the word.\n"; } else { print "ONE does not have the word.\n"; } if (has_word("TWO")) { print "TWO already has the word.\n"; } else { print "TWO does not have the word.\n"; } } main;

    Content of file in this particular case:

    ASDF_ONE { magic tmp tmp } ASDF_TWO { tmp magic tmp } string3 { tmp tmp magic }

    The output is not what I expect:

    ONE already has the word. TWO does not have the word.

    Indeed, all the sections in this case have the word.

      >     local $/;

      That's not how it works, his solution is based on setting $INPUT_RECORD_SEPARATOR to the closed brace "}\n" (while "\n}" would be better in the case of trailing whitespace)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

      This seems to work:

      $ cat file.txt ASDF_ONE { magic tmp tmp } ASDF_TWO { tmp magic tmp } string3 { tmp tmp magic }
      #!/usr/bin/perl use warnings; use strict; use feature 'state'; my $file_name = 'file.txt'; sub get_file_data { state $data; unless ( length $data ) { open my $FH, '<', $file_name or die "Cannot open '$file_name' +because: $!"; my $read = read $FH, $data, -s $FH; $read == -s _ or die "Error reading '$file_name'"; } return $data; } sub has_word { my $query = shift; my $file = get_file_data(); local $/ = "\n}\n"; open my $FH, '<', \$file; while ( <$FH> ) { if ( /^ASDF_\Q$query/ && /magic/ ) { return 1; } } return; } if ( has_word( 'ONE' ) ) { print "ONE already has the word.\n"; } else { print "ONE does not have the word.\n"; } if ( has_word( 'TWO' ) ) { print "TWO already has the word.\n"; } else { print "TWO does not have the word.\n"; }

      And it produces this output:

      $ perl 11129184.pl ONE already has the word. TWO already has the word.