in reply to Re^2: How to extract a pattern in Perl regex?
in thread How to extract a pattern in Perl regex?
Surely there's a simpler way?
Just capture what you want. Let's change the task to remove the elephant in the room of parsing HTML with regex which you now know you shouldn't do. Instead suppose you want to extract everything between 'foo' and 'bar' and ignore all the rest. Here's the simple approach:
use strict; use warnings; use Test::More tests => 1; my $in = 'abcfooHellobarxyz'; my $want = 'Hello'; my ($have) = ($in =~ /foo(.*)bar/); is $have, $want, "Extracted $want";
The only real caveat to this is to remember to use the /s modifier if the text you are extracting might contain \n.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: How to extract a pattern in Perl regex?
by SergioQ (Scribe) on May 01, 2020 at 22:24 UTC | |
|
Re^4: How to extract a pattern in Perl regex?
by AnomalousMonk (Archbishop) on May 01, 2020 at 10:33 UTC | |
by hippo (Archbishop) on May 01, 2020 at 10:50 UTC | |
by AnomalousMonk (Archbishop) on May 01, 2020 at 11:52 UTC | |
by Your Mother (Archbishop) on May 01, 2020 at 15:47 UTC |