in reply to Find Not Working
Right off the bat, your regex will never match, as this: ^\s{32} says "match exactly 32 whitespace characters at the very beginning of the string", but each line starts with a word character (\w). That's not the only issue, but I digress. Try this:
use warnings; use strict; my $find = qr/ ^ # start of string \w+ # one or more word chars (last name) \s+ # one or more whitespace ( # begin capture (goes into $1) (?:H0|HT) # H0 or HT .* # everything to end of string ) # end capture /x; open my $fh, '<', 'in.txt' or die $!; while (<$fh>){ if (/$find/){ my $string = $1; # $1 contains what we captured in the rex print "$string\n"; } }
Output:
HT00000000 I HT00000000 S HT00000000 I HT00000000 M HT00000000 I HT00000000 I H000000000 I H000000000 O H000000000 I
Here's the regex without breaking it up for explanation: /^\w+\s+((?:H0|HT).*)/
Have a read of perlretut and perlre.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Find Not Working
by Marshall (Canon) on Jun 03, 2016 at 12:20 UTC | |
by stevieb (Canon) on Jun 03, 2016 at 19:45 UTC |