Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: Matching a string in a parenthesized block (regex help)

by jwkrahn (Abbot)
on Mar 06, 2021 at 22:04 UTC ( [id://11129217]=note: print w/replies, xml ) Need Help??


in reply to Re^2: Matching a string in a parenthesized block (regex help)
in thread Matching a string in a parenthesized block (regex help)

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11129217]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-26 04:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found