I am trying to use a single regex to capture 2 unique patterns
from a text file. The patterns are not on the same line of text.
---Example Text File Start---
First line of the file used for filler
#augment:A Something One
Another line of text for filler
#augment:B Something Two
Last line of the file used for filler
---Example Text File End---
I want to capture the text 'Something One'
and 'Something Two', from the file using a single regex
Perl script snippet showing problem
open my $pf, '<', $file or die "ERROR: Cannot open input file: $file $ +!\n"; my $newvar; #chomp (my @array = (<$pf>)); my @array = (<$pf>); foreach (@array) { $newvar .= $_; } #Attempt to capture just one pattern $newvar =~ /(?:#augment:A)(.*)(?:\n)/; say $1; #this works, printed to standard output is: ' Something One' as I hope +d for #Attempt to capture just one pattern $newvar =~ /(?:#augment:B)(.*)(?:\n)/; say $1; #this works, printed to standard output is: ' Something Two' as I hope +d for #Attempt to capture 2 patterns by combining above regex's $newvar =~ /(?:#augment:A)(.*)(?:\n)(?:#augment:B)(.*)(?:\n)/ ; say $1; say $2; #Does not work - nothing is captured and I get uninitialized value $1 +and $2 warnings...
Note: I also tried using a while loop to read the file in
line by line and match each line against the combined regex - but that did
not capture both patterns either.
Is it possible to capture both patterns in a case like this using
a single regex expression in perl?
In reply to regex capture group problem by midiperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |