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

Re^2: Regex Doubt.

by Anonymous Monk
on Oct 16, 2012 at 03:12 UTC ( [id://999206]=note: print w/replies, xml ) Need Help??


in reply to Re: Regex Doubt.
in thread Regex Doubt.

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.

Replies are listed 'Best First'.
Re^3: Regex Doubt.
by Anonymous Monk on Oct 16, 2012 at 03:18 UTC

    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.

      my $total_names = "Matthew,Thomas,Peter,Randy,George,Federick"; my $last_name = $1 if ($total_names =~ /(,[^,]+)$/ ); print "$last_name;

      Thanks Monk.

        The error says:

        Can't find string terminator '"' anywhere before EOF

        and your code has:

        print "$last_name; # ^

        Nothing to do with the regex.

        Actually, the regex would be better not capturing the comma, and there is no need to quote the variable at all:

        my $total_names = "Matthew,Thomas,Peter,Randy,George,Federick"; $total_names =~ /,([^,]+)$/; print $1 if $1;

        or

        my $total_names = "Matthew,Thomas,Peter,Randy,George,Federick"; my ($last_name) = $total_names =~ /,([^,]+)$/; print $last_name if $last_name;

        Update: If your teacher does allow you to use split, you’ll get the last substring by subscripting with -1:

        my $last_name = ( split /,/, $total_names ) [ -1 ] ;

        (A [ 0 ] subscript gives you the first substring.)

        Hope that helps,

        Athanasius <°(((><contra mundum

        print "$last_name;
              ^----------^
              |          \--missing quote
        first quote
        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://999206]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-03-29 12:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found