Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Check if a variable contains only one letter?

by Anonymous Monk
on May 02, 2014 at 14:01 UTC ( [id://1084787]=perlquestion: print w/replies, xml ) Need Help??

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

Hello, is there a way to check if a given variable contains only one letter, e.g. X and not any other letters from A-Z? By doing:
if($myvar=~/X+/) { print "ERROR\n"; }
it works but in that case, as you know, $myvar can contain other letters...

Replies are listed 'Best First'.
Re: Check if a variable contains only one letter?
by choroba (Cardinal) on May 02, 2014 at 14:05 UTC
    You should say "it contains X from the beginning to the end":
    $myvar =~ /^X+$/;

    Update: Be careful as "XXXX\n" also matches, so you might need to chomp first.

    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Regarding your update, just use /\AX+\z/.

      use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name

      Or if you don't know what the letter is ...

      if($myvar =~ /^([A-Za-z])\1*$/) { ... }

      That is, match the beginning of the string, then any letter, then any more of that same letter, then the end of the string. Adapt as appropriate for Unicode abominations.

Re: Check if a variable contains only one letter?
by roboticus (Chancellor) on May 02, 2014 at 14:12 UTC
    if ($myvar =~ /^[^A-VYZ]*(X+[^A-VYZ]*)+$/) { print "Error\n"; }

    This should let you match things like 9XX35X7 and still reject AXXBXC.

    Update: It's really too bad that I don't know the alphabet, and I inadvertently left W out. ww pointed it out to me, and I didn't intentionally omit him!

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: Check if a variable contains only one letter?
by hdb (Monsignor) on May 02, 2014 at 14:05 UTC

    Use anchors: /^X+$/

Re: Check if a variable contains only one letter?
by Anonymous Monk on May 02, 2014 at 14:36 UTC

    As evidenced by the varied responses from the other monks, your problem statement is somewhat unclear.

    Perhaps this is what you're looking for?

    #!/usr/bin/env perl use warnings; use strict; while (my $myvar=<DATA>) { chomp($myvar); if ($myvar=~/^([A-Z])\1*$/) { print "'$myvar' matches\n"; } else { print "'$myvar' doesn't match\n"; } } __DATA__ X XXXX XXXY XXYX YXXX AAAA BBBB BBBC DDD4

    Output:

    'X' matches 'XXXX' matches 'XXXY' doesn't match 'XXYX' doesn't match 'YXXX' doesn't match 'AAAA' matches 'BBBB' matches 'BBBC' doesn't match 'DDD4' doesn't match
Re: Check if a variable contains only one letter?
by oiskuu (Hermit) on May 02, 2014 at 18:10 UTC

    To test if a variable contains a repeat sequence of some letter, any one letter:

    if ($myvar =~ /^([[:alpha:]])\1*\z/) { ... }

Re: Check if a variable contains only one letter?
by toolic (Bishop) on May 02, 2014 at 14:05 UTC
    UPDATE: Nevermind... I misread the question. I thought the variable was of length 1. It would have helped had the OP shown any input at all.

    Use eq instead of a regex:

    use warnings; use strict; while (<DATA>) { chomp; print "ERROR $_\n" unless $_ eq 'X'; } __DATA__ X XA XB
      > It would have helped had the OP shown any input at all.

      agreed, I also wasted time answering another question. :(

      > Check if a variable contains only one letter

      DB<106> $str='A B C' => "A B C" DB<107> @matches= $str=~/([A-Z])/g => ("A", "B", "C") DB<108> scalar @matches => 3

      DB<115> $str='A B C' => "A B C" DB<116> $str !~ /[A-Z].*[A-Z]/ => "" DB<117> $str=" A " => " A " DB<118> $str !~ /[A-Z].*[A-Z]/ => 1

      Cheers Rolf

      ( addicted to the Perl Programming Language)

Re: Check if a variable contains only one letter?
by Yary (Pilgrim) on May 02, 2014 at 15:56 UTC
    (edit) I have to now agree with the other posters, that my interpretation of the question is just one of many. The below is a simple answer to one of those interpretations.

    I can't believe everyone's missed the obvious!

    $myvar =~ /X/i && $myvar !~ /[A-WYZ]/i

    If you want to try putting it all in one regexp, see this meditation- Matching and nonmatching multiple regexps at once

    And if you want it to be Unicode-safe, not just matching ASCII- I'm sure it can be and should be done, but I'm not the one to ask...

    (edit #2) And if you really want to know if a string is a single letter X then don't use regexp. $myvar eq 'X'

Re: Check if a variable contains only one letter?
by wjw (Priest) on May 02, 2014 at 15:44 UTC
    So tell me if this is what you are looking for:

    I want to know if a variable containing a string of unknown number of characters consists exclusively of the indicated character.

    Is that what you are looking for?

    Does upper/lower case matter?
    Do numerics count?

    ...the majority is always wrong, and always the last to know about it...
    Insanity: Doing the same thing over and over again and expecting different results...
Re: Check if a variable contains only one letter?
by Anonymous Monk on May 02, 2014 at 14:04 UTC
    Is that the solution or it can be done easier?
    $string="UUUUUUUUUUUUUUUUUU"; if ($string=~/U+/ && $string =~ m/[^ABCDEFGHIJKLMNOPQRSTVWXYZ]/){ print "FOUND\n"; }

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (5)
As of 2024-04-19 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found