Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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 pondering the Monastery: (3)
As of 2014-09-24 03:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (245 votes), past polls