Re: Perl 5.8 RE for fixed string with an optional colon terminated prefix



On Thu, 8 Apr 2010 17:22:48 +0200, Helmut Richter <hhr-m@xxxxxx> wrote:

On Thu, 8 Apr 2010, RedGrittyBrick wrote:

if ($x =~ /^[^:]*:?\bFOO$/) {

That will still independetly check for optional [^:]* and optional :
letting ,,,FOO pass. What you mean is probably /^([^:]+:)?\bFOO$/.
Here I assumed that the part before the colon is nonempty if the colon is
present.


this - /^([^:]+:)?\bFOO$/

Indeed, grouping the the optional [^:]* and optional :
into a ()? took care of [^:]* letting ,,,FOO pass

However, by doing that, you don't let "prefix.FOO" pass.
This is why he had the :? independently optional so that

^[^:]*:?\bFOO$ would match 'prefix.FOO'

The \b protects the '.FOO' and the 'FOO' at the same time.
where [^:]*:? specifically matches 'prefix:FOO' and just so
happens : is compliant with \b and redundant.

Grouping changes the condition. Now \b won't find '.FOO'
because the search never consumes a '.'

This /^([^:]+:)?\bFOO$/ regexp at best is bloviated and redundant,
at worst will not match the conditions.

-sln
.