tags:

views:

93

answers:

3

The word starts with -D(It may be d also) Followed with..IT may contain spaces or do on not caontain After that its Followed by a $ Then the series is{rajna_NAME} To be brief

-d ${ranjana_wdgf_NAME}or -D${Tom_task_NAME}

i want to extract

ranjana_wdgf
Tom_task

My doubt is

if($word =~m/^-D(\$)\|(\w*NAME)\b/)

Whats wrong: Please provide suggestion

+1  A: 

Sometimes regexes are not as robust as other means.

For this case, I wouldn't mind giving Text::Balanced a try.

Zaid
+2  A: 

If you really need a Regular Expression to solve the problem, then try this one

$word =~ m/\{(.*?)\_NAME/; # assuming that name is surrounded by curly brackets

Updated:

$word =~ m/-[dD]\s*\$\{(.*?)_NAME/; #assuming -d or -D always be there.
Nikhil Jain
Is there any chance to include -D or -d in the regex as a filtering criterial allong with it should start with..Because its filtering sum other unwanted data..since the text file is large i am not able to provide the entire content
Sreeja
@Sreeja: updated, have a look.
Nikhil Jain
@NikhilThanks..Its working fine with this..
Sreeja
+1  A: 

I'm not sure I understood the question properly. But looks like the code below solves your problem:

$ cat extract.pl 
#!/usr/bin/perl

use strict;
use warnings;

sub extract_name($)
{
  my $s = shift;
  if($s =~ /^-[dD]\s*\${(.*)_NAME}$/)
    {
      print "$1\n";
    } 
  return;
}

extract_name('-d ${ranjana_wdgf_NAME}');
extract_name('-D${Tom_task_NAME}');
$ perl extract.pl 
ranjana_wdgf
Tom_task
$
Dmitry V. Krivenok
@dmitryAwesome..Have few more doubts to be clarified with you
Sreeja
why do we use shift?
Sreeja
Are you talking about "shift" in this example?It is used to access parameters passed to subroutine.Alternatively, you can access parameters directly via $_[0], $_[1], etc.
Dmitry V. Krivenok