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.
| [reply] [d/l] [select] |
|
| [reply] [d/l] |
|
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.
| [reply] [d/l] |
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. | [reply] [d/l] |
Re: Check if a variable contains only one letter?
by hdb (Monsignor) on May 02, 2014 at 14:05 UTC
|
| [reply] [d/l] |
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
| [reply] [d/l] [select] |
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/)
{
...
}
| [reply] [d/l] |
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
| [reply] [d/l] [select] |
|
| [reply] [d/l] [select] |
Re: Check if a variable contains only one letter?
by Yary (Pilgrim) on May 02, 2014 at 15:56 UTC
|
$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' | [reply] [d/l] [select] |
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...
| [reply] |
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";
}
| [reply] [d/l] |