check if a string has any capital letters? 
Author Message
 check if a string has any capital letters?

Hi,

Besides 'if ($string =~ /[A-Z]/)', any other ways to check whether or
not $string has any capital letters?

Thanks,

Bing



Mon, 17 May 2004 17:46:57 GMT  
 check if a string has any capital letters?


Quote:
> Hi,

> Besides 'if ($string =~ /[A-Z]/)', any other ways to check whether or
> not $string has any capital letters?

> Thanks,

> Bing

Well, so long as you're sticking to ASCII characters...

my $string = 'AbyZ';


   if(ord($char) >= 65 && ord($char) <= 90){
      print "Cap found: $char\n";
   }

Quote:
}

Regards,

Mr. Sunblade



Mon, 17 May 2004 19:51:12 GMT  
 check if a string has any capital letters?

Quote:

> Besides 'if ($string =~ /[A-Z]/)', any other ways to check whether or
> not $string has any capital letters?

if ($string =~ tr/A-Z// > 0)
 { print "Boss, I found at least one capital letter. Now what?"; }

Godzilla!



Mon, 17 May 2004 20:04:33 GMT  
 check if a string has any capital letters?

Quote:

> Hi,

> Besides 'if ($string =~ /[A-Z]/)', any other ways to check whether or
> not $string has any capital letters?

You could use this sort of count-functionality of tr/// or y///
respectively:

my $s = "This String Has 5 Capital Letters";
my $num = $s =~ tr/A-Z//;
print "Caps: $num\n";
__END__
Caps: 5

Thus you'd avoid a regex. You can make this the if-condition as well:

if (my $num = $s =~ tr/A-Z//) {
    print "has caps\n";
    ...

Quote:
}

Tassilo
--
Neckties {*filter*} clear thinking.
                -- Lin Yutang


Mon, 17 May 2004 20:16:19 GMT  
 check if a string has any capital letters?

Quote:
> Besides 'if ($string =~ /[A-Z]/)', any other ways to check whether or
> not $string has any capital letters?

Your approach fails for any character outside of the 7bit-ASCII range.

What about
    if ($string eq lc($string)) {
        "Does not contain uppercase characters";
    } else {
        "Contains uppercase characters";
    }

This one works for all characters, including non-ASCII characters.

jue



Mon, 17 May 2004 20:58:32 GMT  
 check if a string has any capital letters?

Quote:

> > Besides 'if ($string =~ /[A-Z]/)', any other ways to check whether or
> > not $string has any capital letters?

> if ($string =~ tr/A-Z// > 0)
>  { print "Boss, I found at least one capital letter. Now what?"; }

Doesn't work for characters outside of the 7-bit ASCII range (just as the
original idea, too, btw).

jue



Mon, 17 May 2004 21:00:03 GMT  
 check if a string has any capital letters?

Quote:



> > Besides 'if ($string =~ /[A-Z]/)', any other ways to check whether or
> > not $string has any capital letters?

> Your approach fails for any character outside of the 7bit-ASCII range.

> What about
>     if ($string eq lc($string)) {
>         "Does not contain uppercase characters";
>     } else {
>         "Contains uppercase characters";
>     }

> This one works for all characters, including non-ASCII characters.

> jue

Hmm....using my Sun keyboard I tried this:

#!/usr/local/bin/perl -w
use strict;

my $string = "mller"; # Used "compose" key

my $lc = lc($string);
print "Lowered is: $lc\n";

But it didn't seem to lower case the .   When I printed the ord value, it
came out as 220, instead of the expected 154.

Dammit Sun!

Any Sun/Solaris users out there (or anyone, really) have any suggestions
or comments?  I mean, it's certainly possible that you might be parsing a
file that contains extended ASCII characters generated this way.

Regards,

Mr. Sunblade

--
"Evil will always triumph because Good is *dumb*."
-- Dark Helmet, 'Spaceballs: The Movie'



Mon, 17 May 2004 21:29:22 GMT  
 check if a string has any capital letters?

Quote:
> Hmm....using my Sun keyboard I tried this:

> #!/usr/local/bin/perl -w
> use strict;

> my $string = "mller"; # Used "compose" key

> my $lc = lc($string);
> print "Lowered is: $lc\n";

> But it didn't seem to lower case the .   When I printed the ord value, it
> came out as 220, instead of the expected 154.

Did you
    use locale;
?

jue



Mon, 17 May 2004 21:31:03 GMT  
 check if a string has any capital letters?

Quote:


>> if ($string =~ tr/A-Z// > 0)
>>  { print "Boss, I found at least one capital letter. Now what?"; }

> Doesn't work for characters outside of the 7-bit ASCII range (just as the
> original idea, too, btw).

Character-classes should be locales-tight:


$string = "??alkls";
$num = $string =~ tr/A-Z??//;
print $num;
__END__
4

For those not having installed the right character-sets:
There are two AE, one UE and one EURO-sign in $string and tested upon
with the transliteration.

I am however not so sure how to deal with Unicode-stuff.

Tassilo
--
Imagination is more important than knowledge.
                -- Albert Einstein



Mon, 17 May 2004 21:37:06 GMT  
 check if a string has any capital letters?

Quote:



> > > Besides 'if ($string =~ /[A-Z]/)', any other ways to check whether or
> > > not $string has any capital letters?
> > Your approach fails for any character outside of the 7bit-ASCII range.
> > What about
> >     if ($string eq lc($string)) {
> >         "Does not contain uppercase characters";
> >     } else {
> >         "Contains uppercase characters";
> >     }
> > This one works for all characters, including non-ASCII characters.

This code of Exner's produces zero results.

Quote:
> Hmm....using my Sun keyboard I tried this:
> #!/usr/local/bin/perl -w
> use strict;
> my $string = "mller"; # Used "compose" key
> my $lc = lc($string);
> print "Lowered is: $lc\n";
> But it didn't seem to lower case the .   When I printed the ord value, it
> came out as 220, instead of the expected 154.

#!perl

print "Content-type: text/plain\n\n";

$string = "?????????D??????YT?";

my $lc = lc($string);
print "Lowered is: $lc\n\n";

$string = "?????????D??????YT?";

if ($string eq lc($string))
 { print "Does not contain uppercase characters"; }
else
 { print "Contains uppercase characters"; }

PRINTED RESULTS:
________________

Lowered is: ?????????D??????YT?

Does not contain uppercase characters

Godzilla!
--

[Do not pay any attention to what Godzilla says. It is a troll, and
has no decent working knowledge of Perl or  programming in general.
Search groups.google.com to see a history  of its posts and replies
to these posts.]



Mon, 17 May 2004 21:47:18 GMT  
 check if a string has any capital letters?

Bing> Besides 'if ($string =~ /[A-Z]/)', any other ways to check whether or
Bing> not $string has any capital letters?

An exhaustive list would have to include:

        if ($x ne lc $x) { ... }

:-)

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095

Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!



Mon, 17 May 2004 21:53:46 GMT  
 check if a string has any capital letters?


Quote:


> > Hmm....using my Sun keyboard I tried this:

> > #!/usr/local/bin/perl -w
> > use strict;

> > my $string = "mller"; # Used "compose" key

> > my $lc = lc($string);
> > print "Lowered is: $lc\n";

> > But it didn't seem to lower case the .   When I printed the ord value,
it
> > came out as 220, instead of the expected 154.

> Did you
>     use locale;
> ?

> jue

Ok - added "use locale" right after "use strict".  Should work for the
entire program, right?  Same results.

"perl -V:d_setlocale" came back as "d_setlocale='define';" as expected.

I guess I don't have my environment set up correctly for locale, however.
Good to know.  Damn - now I have to read *more* Solaris documentation.

Regards,

Mr. Sunblade



Mon, 17 May 2004 21:58:05 GMT  
 check if a string has any capital letters?

(snipped)

Quote:

> Hmm....using my Sun keyboard I tried this:

> #!/usr/local/bin/perl -w
> use strict;

> my $string = "mller"; # Used "compose" key

> my $lc = lc($string);
> print "Lowered is: $lc\n";

> But it didn't seem to lower case the .   When I printed the ord value, it
> came out as 220, instead of the expected 154.

> Dammit Sun!

> Any Sun/Solaris users out there (or anyone, really) have any suggestions
> or comments?  I mean, it's certainly possible that you might be parsing a
> file that contains extended ASCII characters generated this way.

According to 'perldoc perllocale':

     o   Regular expressions and case-modification functions
         (uc(), lc(), ucfirst(), and lcfirst()) use `LC_CTYPE'

Read perllocale then try this:

#! /usr/local/bin/perl

use strict;
use warnings;
use locale;
my $string = "mller";

my $locale = `/usr/local/bin/perl -V:d_setlocale`;

if ($locale =~ /'define';$/) {
    use POSIX qw(locale_h);
    # check your own system for valid LC_CTYPE
    setlocale(LC_CTYPE, "de_AT.ISO8859-15");

Quote:
}

{
    local $\ = "\n";
    print lc($string);
    print join $", map { ord } split(//,lc($string));

Quote:
}

--
Cheers,
Steve


Mon, 17 May 2004 22:00:16 GMT  
 check if a string has any capital letters?

[...]

Quote:
> According to 'perldoc perllocale':

>      o   Regular expressions and case-modification functions
>          (uc(), lc(), ucfirst(), and lcfirst()) use `LC_CTYPE'

> Read perllocale then try this:

And you please check your available character-sets. :-)

Quote:
> Content-Type: TEXT/PLAIN; charset=X-UNKNOWN

                            ^^^^^^^^^^^^^^^^^

cause otherwise the following happens:

Quote:
> my $string = "m?ller";

                 ^
Usually this would not do harm but when talking about locales this is a
sensitive point I think. ;-)

Tassilo
--
Teutonic:
        Not enough gin.



Mon, 17 May 2004 22:06:11 GMT  
 check if a string has any capital letters?

Quote:
> Ok - added "use locale" right after "use strict".  Should work for the
> entire program, right?  Same results.

"use locale" only tells Perl to use(!) the locale specific information.
However you still need to define(!) which locale you actually want to use
(see perldoc perllocale) for more details.

On a side note:
Using the character range [A..Z] to find all capital letters in German
should actually work (provided the locale is set correctly to German)
because all German umlauts are between A and Z according to the German
alphabet.
However it will fail for e.g. Swedish, because the Swedish alphabet sorts
them after the Z.

jue



Mon, 17 May 2004 22:11:09 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. small letters into capital letter

2. Initial Capital Letters

3. Capital Letters Won't Sort - HeLp!

4. Translating first letter of sentence into a capital

5. checking for non-letters

6. NEWBIE: Checking a string against another string

7. Recognizing first letter in a string

8. how to substitute letters within a string

9. Getting the First Letter in string

10. Regular Expression: Remove Double Letters from a String?

11. Get first letter of string?

12. UpperCase first letter of string only.

 

 
Powered by phpBB® Forum Software