Sanket Sanket - 23 days ago 9
Perl Question

how can I get just ahead string using Regular expression where matching string is two time using perl?

I am trying to get necessary string by applying reg-ex on whole word, here is my word:


Birla Sun Life Global Real Estate Fund - Retail Plan - Direct Plan -
Growth Option


SBI GOLD FUND - DIRECT PLAN - DIVIDEND

I want to get
Plan
type which is
Direct


and I have words ,some have two times plan and some have single time so regex should be work for both
Here is my Code example :

$pname = 'Birla Sun Life Global Real Estate Fund - Retail Plan - Direct Plan - Growth Option';
if($pname =~/ ([^\s]*) plan(?!^plan$)*/ig) #reg-ex for get plantype
{
$plan_type=$1;
}
print"$1";


But it give output
Retail
instead of
Direct
,
So what should I do So that I can get
Direct
as
Plan
type?

Answer

Try this:

(\w+)\s+Plan(?!.*Plan)

Explanation

Run the perl code here

use strict;

my $str = 'Birla Sun Life Global Real Estate Fund - Retail Plan - Direct Plan - Growth Option\';  ';
my $regex = qr/(\w+)\s+Plan(?!.*Plan)/p;

if ( $str =~ /$regex/g ) {
  print "$1";
}
Comments