Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Check if a variable contains only one letter?

by choroba (Cardinal)
on May 02, 2014 at 14:05 UTC ( #1084789=note: print w/replies, xml ) Need Help??


in reply to Check if a variable contains only one letter?

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.

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

Replies are listed 'Best First'.
Re^2: Check if a variable contains only one letter?
by tobyink (Canon) on May 02, 2014 at 22:59 UTC

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

    use Moops; class Cow :rw { has name => (default => 'Ermintrude') }; say Cow->new->name
Re^2: Check if a variable contains only one letter?
by DrHyde (Prior) on May 06, 2014 at 10:38 UTC

    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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2023-02-09 13:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer not to run the latest version of Perl because:







    Results (45 votes). Check out past polls.

    Notices?