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

Hi guys !

I have a string (signifies state), for example siteConfigView or siteConfigDelete, and I want to take only the last word that starts with capital letter, in this case View and Delete, is there a way for doing that?

Thanks.

Hotshot
  • Comment on Looking for a regex to match last capitalized word in string

Replies are listed 'Best First'.
Re: regexps
by broquaint (Abbot) on Mar 26, 2002 at 16:01 UTC
    A possible regex would be
    my $str = "siteConfigView"; my($last_word) = $str =~ /([A-Z][a-z]+)$/; print $last_word, $/;
    This looks for a capital letter followed by at least one lowercase letter at the end of the string, nice and straight-forward really :-)
    HTH

    broquaint

(jeffa) Re: regexps
by jeffa (Bishop) on Mar 26, 2002 at 16:03 UTC
    Here is one way:
    use strict; for (qw(siteConfigDelete siteConfigView)) { my ($last) = $_ =~ /([A-Z][a-z]+)$/; print "$last\n"; }

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: regexps
by lestrrat (Deacon) on Mar 26, 2002 at 16:06 UTC

    Assuming that the last word always consists of one capital letter, and then a series of lower case letters until the end of the string, you could do

    my $foo = "siteConfigDelete"; my( $last ) = ( $foo =~ /([A-Z][a-z]+)$/ ); print "$last\n";
Re: regexps
by Fletch (Bishop) on Mar 26, 2002 at 16:08 UTC

    Of course if you want to honour any locale settings, /([[:upper:]][[:lower:]]+)$/ might be more appropriate.

Re: regexps
by cchampion (Curate) on Mar 26, 2002 at 16:43 UTC
    Two alternative solutions, saving all the string components.

    The first solution will create an array of the distinct words composing the string.
    The second one will also separate the initial letter. Is it needed? I don't know, but just in case :-).
    $_="ConfigState"; my @words= split /([A-Z][^A-Z\s]+)$/,$_; print @words[-1], "\n"; # prints "State" # @words = qw(Config State) @words= split /([A-Z])/,$_; print @words[-2,-1], "\n"'; # It's a trick, but it prints "State" # @words = qw(C onfig S tate)
Re: regexps
by Sidhekin (Priest) on Mar 26, 2002 at 16:23 UTC

    The other suggestions may be what hotshot wanted, but they are not what he asked for ... not that it is very clear what he is asking for, but you might do well to consider the differences between this and the other solutions:

    for ("siteConfigView", "siteConfigDelete (with ws and lc extras) ") { my $last = (/([A-Z][^\WA-Z]*)/g)[-1]; print "$last\n"; }

    Now, s/\*/+/ if that fits your idea of "word" better. And of course, if your string is really long, this version is likely slow. Use with care :-)

    The Sidhekin
    print "Just another Perl ${\(trickster and hacker)},"

      just for fun...
      for ("siteConfigView", "siteConfigDelete (with ws and lc extras) ") { my $last = (/([A-Z][^\WA-Z]*)/g)[-1]; print "$last\n"; }
      You could collapse the above code into a one-liner: print+(/[A-Z][^\WA-Z]*/g)[-1],$/for"siteConfigView","siteConfigDelete (with ws and lc extras)  " --Dave