I am trying to understand a concept in Regex in Perl. How to write regex in Perl such that metacharacter * is not greedy.
Here is my code:-
Expand|Select|Wrap|Line Numbers
- #!usr/bin/perl
- use strict;
- my $sentence = "Perl is a dynamic programming language created by Larry Wall and first released in 1987,
- Perl is based on the brace-delimited block style of AWK and C,
- and was widely adopted for its strengths in text processing
- and lack of the arbitrary limitations
- of many scripting languages at the time.";
- my $b;
- if ($sentence =~ /and(.*)\./s)
- {
- $b = $1;
- print "The following is the output:-\n";
- print "$b\n";
- }
The following is the output:-
first released in 1987,
Perl is based on the brace-delimited block style of AWK and C,
and was widely adopted for its strengths in text processing
and lack of the arbitrary limitations
of many scripting languages at the time
The * operator is very greedy and so I get the output like that.
I want the output to be just from the last occurence of "and" upto the "." like the following:-
lack of the arbitrary limitations
of many scripting languages at the time
So how do I achieve that? I tried using the repetition modifier {} after "and" but that does not work either.
I would appreciate if you could help me with this.
Thanks in advance,
Sangith