in reply to Get text between start and end tag

It seems to be too greedy.

Quite right. You can make it less greedy by using the ? character.

#!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my $text = <<EOT; Any text is in this document. But I'm only interested in the text between a beginning and an end tag +. BEGIN_TAG_110 In this text I'm interested in. It can consist of several lines. Until the end tag. END_TAG and any other things I'm not interested in until the next begi +nning tag starts. Any other text. BEGIN_TAG_237 I need this text. END_TAG More text ... EOT my %res = $text =~ /BEGIN_TAG_(\d+)(.*?)END_TAG/sg; print Dumper (\%res);

That should get you started anyway.


🦛

Replies are listed 'Best First'.
Re^2: Get text between start and end tag
by Dirk80 (Pilgrim) on Feb 27, 2024 at 11:42 UTC

    Thank you very much!! This solved my problem. I just slurp the text file in a string variable and then apply your regex which I can now fully understand.

    #!/usr/bin/env perl use strict; use warnings; use Data::Dumper; my $file = 'textfile.txt'; open my $fh, '<', $file or die; $/ = undef; my $text = <$fh>; close $fh; my %res = $text =~ /BEGIN_TAG_(\d+)(.*?)END_TAG/sg; print Dumper (\%res);