On Thu, 08 Apr 2010 15:39:04 +0100, RedGrittyBrick <> wrote:
>I need to match fixed string FOO with an optional : terminated prefix
>
>This doesn't work because the RE matches notFOO
>
>for my $x (qw(FOO xxx:FOO yyy.FOO BAR notFOO FOOM)) {
> if ($x =~ /^[^:]*
FOO$/) {
^^^^^^^^
This can't be generalized to be put together without alternation.
^ and [^:]*

need to be alternatives as a prefix to FOO
Because it makes you have to say [^:]*

and the quantifiers
* ? make posibile any char not :* zero or more times then

zero or one time.
This makes it too particular in its need to find : and results
in everything else not : getting by and matching. Even if you used
[.:] it wouldn't work.
From your own description, you positively are looking for only
2 things to match:
1. FOO all by itself
2. FOO prefixed by anything plus : or .
Thats the way you have to write the regexp.
Lets also asume that 1 and 2 above can be surrounded by optional
whitespace:
1. FOO all by itself
/ ^ \s* FOO \s* $ /x
2. FOO prefixed by anything plus : or .
/ .+[.:] FOO \s* $ /xs
Join them together via factoring:
/ (?: ^\s* | .+[.:] ) FOO \s* $/xs
> print "Matched $x\n";
> } else {
> print "- $x\n";
> }
>}
>
Matched () FOO
Matched (xxx

xxx:FOO
Matched (yyy.) yyy.FOO
- BAR
- notFOO
- FOOM
-----------------------
use strict;
use warnings;
for my $x (qw(FOO xxx:FOO yyy.FOO BAR notFOO FOOM)) {
if ($x =~ / ( ^\s* | .+[.:] ) FOO \s* $/xs) {
print "Matched ($1) $x\n";
} else {
print "- $x\n";
}
}
__END__
-sln