Pergunta

From the following word "tacacatac", I want to match "cat". It seems like the regex c.*?t should give me this, but I guess it starts with the first occurrence of "c" and then from there finds the next "t", and thus, matches "cacat".

Is there a way to (perhaps using negative lookahead) start looking from the c just before the final t?

-----edit----- I need an option that will work if you replace the letters with strings

Thanks.

Foi útil?

Solução 2

try this:

my $str = "the cat in the cat in the hat";
if ($str =~ /(cat(?:(?!cat).)*hat)/) {
    print $1, "\n";
}

Outras dicas

You can use negated character class:

c[^ct]*t

This will match any character but c and t in between.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top