in reply to Conditional regex

You probably want this instead ...
#!/usr/local/bin/perl -w use strict; my @words = qw/ beriberi coco couscous deed toot toto tutu assa saaa /; for (@words) { print "$_\n" if /^(\w+)\1|(\w+)(\w+)\3\2$/ # $x$x | $x$y$y$x }

Replies are listed 'Best First'.
Re: Conditional regex
by Abigail-II (Bishop) on Jan 12, 2004 at 12:52 UTC
    /^(\w+)\1|(\w+)(\w+)\3\2$/ matches ssaa.

    Abigail

      But I thought 'ssaa' matches the '$x$y' pattern, where $x is 'ss' and $y is 'aa', that still fits the requirement?

      Thanks Abigail-II, I have realized my mistake. Here's an updated version that works -
      #!/usr/local/bin/perl -w use strict; my @words = qw/ beriberi coco couscous deed toot toto tutu assa ssaa /; for (@words) { print "$_\n" if /^(\w+)\1$/ || /^(\w+)(\w+)\2\1$/; }

      And output -
      beriberi coco couscous deed toot toto tutu assa

        Oh, in that case, /^(\w+)\1|(\w+)(\w+)\3\2$/ doesn't match saaa which is part of your own test case. You know, just pick $x = "sa" and $y = "aa", so it matches this new "$x$y" requirement.

        Don't you read your own comments? "$x$x" or "$x$y$y$x". Not "$x$y", which could be matched by /^\w{2,}$/.

        Abigail