Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Help with a regex

by marco.shaw (Initiate)
on Sep 07, 2007 at 14:03 UTC ( #637656=perlquestion: print w/replies, xml ) Need Help??

marco.shaw has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl my $string="testing.123"; my $result = substr( $string, rindex( $string, '.' ) + 1 ); print "$result\n";
Using this, if $string doesn't have ".123" in it, I get the whole string output, but I don't want that. I just want the string output *if* it has ".123" at the end. I guess what I'm looking for is a test as to whether there's a ".0-9*" expression at the end. I need a ".", then any number of numerical values, but at least one. The "." has to be at the end.

Replies are listed 'Best First'.
Re: Help with a regex
by grinder (Bishop) on Sep 07, 2007 at 14:46 UTC

    Your substr didn't work because the code doesn't check to see if rindex found the '.' it was looking for. If you do that, you don't need a regexp at all.

    if ((my $last_dot = rindex($string, '.')) > -1) { $result = substr( $string, 0, $last_dot + 1 ); }

    ... just in case you wanted to know why your code wasn't working as expected.

    • another intruder with the mooring in the heart of the Perl

Re: Help with a regex
by zer (Deacon) on Sep 07, 2007 at 14:23 UTC
    #!/usr/bin/perl my $string="testing.123"; $string =~ /\.([0-9]+)$/; $result = $1; print "$result\n";
Re: Help with a regex
by tirwhan (Abbot) on Sep 07, 2007 at 14:23 UTC

    Use capturing parentheses:

    #!/usr/bin/perl my $string="testing.123"; my ($result) = $string =~ m/(.*?\.)[0-9]+/; print "$result\n" if $result;

    All dogma is stupid.
Re: Help with a regex
by ikegami (Patriarch) on Sep 07, 2007 at 14:48 UTC
    If you want to stick to the string operations:
    my $pos = rindex( $string, '.' ); my $result = ($pos >= 0 ? substr( $string, $pos+1 ) : '' );
Re: Help with a regex
by WiseGuru (Initiate) on Sep 08, 2007 at 08:27 UTC
    try

    my $string="testing.123";
    my @Strings;
    @Strings=split(/\./,$string);
    if($Strings[$#Strings] eq "123"){ DoSomething.... }

    this way you have got the last chunk of the string divided by a period, always, because if there is more than one period then you might not get what you want, but if you have ONLY ONE period and can garrantee it, then use

    my $string="testing.123";
    my $string2;
    ($string,$string2)=split(/\./,$string);
    if($string2 eq "123"){ DoSomething.... }

    good luck!

    www.arobcorp.com

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2022-09-25 03:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (116 votes). Check out past polls.

    Notices?