Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Regex Doubt.

by Anonymous Monk
on Oct 16, 2012 at 03:00 UTC ( #999204=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks, I have a doubt regarding Perl Regex.

my $total_names = "Matthew,Thomas,Peter,Randy,George,Federick";

my $last_name = $1 if ($total_names =~ /,(.*)/i);

print "$last_name;

I want to print the last name "Federick" only. but now its printing from "Thomas to Federick". Pls can you suggest a way.(using regex). Thanks in advance Monks.

Comment on Regex Doubt.
Re: Regex Doubt.
by Anonymous Monk on Oct 16, 2012 at 03:03 UTC
    Use an anchor and negated character class (match comma followed by not comma followed by end of line)
    =~ /(,[^,]+)$/
    or use split
    my $last = ( split /,/, $str ) [ 0 ] ;
    See perlrequick it has several examples

      I have no permission to do it with split.

      The Regex way (=~ /(,^,+)$/) is giving me an error like the below,

      Can't find string terminator '"' anywhere before EOF at C:\Users\Raul\AppData\Local\Temp\dir6E94.tmp\Untitled line 4.

        I have no permission to do it with split.

        Sure you do

        The Regex way (=~ /(,^,+)$/) is giving me an error like the below,

        Not possible. If you want help, show your code.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://999204]
Approved by Athanasius
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (14)
As of 2014-07-28 18:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (206 votes), past polls