in reply to Multiline regex

This looks to work for me on your sample data:

#!/usr/bin/env perl use strict; use warnings; my $data = 'unwanted_line1=blabla unwanted_line2=blabla my_variable=important_content_section1 important_content_section2 important_content_section3 unwanted_line3=blabla '; my ($getVariable) = $data =~ /(my_variable=.*)^\w+=/sm; print $getVariable;

Update: See Eily's first reply below. If unwanted_line3 is not the last unwanted line in the set after all, then you'll need a non-greedy grab on the capture group like so:

#!/usr/bin/env perl use strict; use warnings; my $data = 'unwanted_line1=blabla unwanted_line2=blabla my_variable=important_content_section1 important_content_section2 important_content_section3 unwanted_line3=blabla nonsense unwanted_line4=blabla whocansay '; my ($getVariable) = $data =~ /(my_variable=.*?)^\w+=/sm; print $getVariable;

Replies are listed 'Best First'.
Re^2: Multiline regex
by Eily (Monsignor) on Jun 22, 2016 at 12:31 UTC

    This works in this case because the next '=' after the required variable also happens to be the last. .*? instead of .* will make sure perl finds the first '=' after the match on "my_variable". It's simpler than my solution with look ahead assertion though, guess I overdid it :)

Re^2: Multiline regex
by adrya407 (Novice) on Jun 22, 2016 at 12:39 UTC
    Due to confidentiality reasons i can't post the data i'm working on, it works almost perfect, except it still gets the unwanted_line3=text but not other lines after that, so it just needs a slight adjustement. Thank you kindly, sir!

      Did you add in the question mark as Eily suggested? If you can't provide an equivalent sample dataset which shows the problem it's going to be very difficult to assist you further, unfortunately.

        Yes, that was the problem, the greedy capture was including that line too. thank you, again!
Re^2: Multiline regex
by Anonymous Monk on Jun 22, 2016 at 12:45 UTC

    Fails for:

    my $data = 'unwanted_line1=blabla unwanted_line2=blabla my_variable=important_content_section1 important_content_section2 important_content_section3 unwanted_line3=blabla unwanted_line4=blabla ';

    Get extra unwanted lines

Re^2: Multiline regex
by Anonymous Monk on Jun 22, 2016 at 13:26 UTC

    Fails if no lines after wanted data, like so:

    my $data = <<END; unwanted_line1=blabla unwanted_line2=blabla my_variable=important_content_section1 important_content_section2 important_content_section3 END

    Fails to match.