Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How to extract the substring from a delimiter (dot) to end of string?

by Anonymous Monk
on May 08, 2000 at 09:29 UTC ( [id://10575]=perlquestion: print w/replies, xml ) Need Help??

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

Given a string like

$string = "helloyoume.she";
How would you extract the characters from the dot (aka period, aka full stop) to the end of that string into a variable? That is, in this example, get the substring "she".

Originally posted as a Categorized Question.

  • Comment on How to extract the substring from a delimiter (dot) to end of string?
  • Download Code

Replies are listed 'Best First'.
Re: How to extract the substring from a delimiter (dot) to end of string?
by infoninja (Friar) on May 08, 2000 at 17:49 UTC

    Here's a solution which doesn't use regexes, which are probably overkill for this application:

    $extension = substr( $string, rindex( $string, '.' ) + 1 );
Re: How to extract the substring from a delimiter (dot) to end of string?
by Novician (Novice) on May 09, 2000 at 12:17 UTC

    How about using split:

    $extension = (split /\./, $string)[-1];
Re: How to extract the substring from a delimiter (dot) to end of string?
by httptech (Chaplain) on May 08, 2000 at 14:41 UTC

    Here's one way to do it, iterating backwards over the string as an array, then unshifting each char into another array until reaching the dot. Of course, you wouldn't normally want to do it this way in practice.

    my @temp; for ( reverse @{[ split( '', $string ) ]} ) { last if /\./; unshift @temp, $_; } $extension = join '', @temp;
Re: How to extract the substring from a delimiter (dot) to end of string?
by ZZamboni (Curate) on May 08, 2000 at 21:56 UTC

    In the spirit of TIMTOWTDI, you could also do:

    ( $extension ) = $string =~ /([^.]*)$/;

    The above will give you the whole string if it does not contain any dots. If you want the result to be undef if there are no dots, you could use:

    ( $extension ) = $string =~ /\.([^.]*)$/;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (8)
As of 2024-04-23 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found