Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Extract Sub String

by gvinu4u (Acolyte)
on Jun 21, 2011 at 06:04 UTC ( #910687=perlquestion: print w/replies, xml ) Need Help??

gvinu4u has asked for the wisdom of the Perl Monks concerning the following question:

# I want to extract the string from start till "_RVCT22" in the mentioned string, please help me with the same. The below program does not give me any output
$string = "Viood_ram_shiva_RVCT22"; if ($string =~/(.*?)\"_RVCT"/) { print "The file name is $1. "; }

Replies are listed 'Best First'.
Re: Extract Sub String
by Ratazong (Monsignor) on Jun 21, 2011 at 06:08 UTC
    if ($string =~/(.*?)\"_RVCT"/)
    Why do you have the \" in your regex? Remove it! And while you are at it, also remove the other " in your regex (that is not part of your $string itself, but the delimiter for perl)
      Thanks a lot I did exactly what you told
Re: Extract Sub String
by alfie (Pilgrim) on Jun 21, 2011 at 07:12 UTC
    Actually you might want to take a look at Death to Dot Star! why you shouldn't use .* in regexps, not even with ?. Also, there is no " in your screen, so there is no need to match it. Also, " is not a special character inside a regex. As a general advice, you might want to read up the perlre manual again.

    use signature;
    signature(" So long\nRhonda");
Re: Extract Sub String
by toolic (Bishop) on Jun 21, 2011 at 15:00 UTC
    Tip #9 from the Basic debugging checklist is to use YAPE::Regex::Explain to demystify Perl regular expressions:
    The regular expression: (?-imsx:(.*?)"_RVCT") matches as follows: NODE EXPLANATION ---------------------------------------------------------------------- (?-imsx: group, but do not capture (case-sensitive) (with ^ and $ matching normally) (with . not matching \n) (matching whitespace and # normally): ---------------------------------------------------------------------- ( group and capture to \1: ---------------------------------------------------------------------- .*? any character except \n (0 or more times (matching the least amount possible)) ---------------------------------------------------------------------- ) end of \1 ---------------------------------------------------------------------- "_RVCT" '"_RVCT"' ---------------------------------------------------------------------- ) end of grouping ----------------------------------------------------------------------
      Thanks to all finally I got this working
      my ( $filename ) = $line =~ /(.*?)_RVCT/;
Re: Extract Sub String
by sanku (Beadle) on Jun 21, 2011 at 11:52 UTC
    Hi, Try this one...
    $string = "Viood_ram_shiva_RVCT22"; if ($string =~/(\w+)(_RVCT)/) { print "The file name is $1. "; }
      if ($string =~/(\w+)(_RVCT)/)
      There is no need for the capturing parentheses around _RVCT:
      use warnings; use strict; my $string = "Viood_ram_shiva_RVCT22"; if ($string =~ /(\w+)_RVCT/) { print "The file name is $1. "; }

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://910687]
Approved by moritz
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2022-05-26 23:01 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (94 votes). Check out past polls.