Question

I know that the regex q(?!u) means q not followed by u.

But what if I encounter a regex (?!u)? Does it mean a null not followed by u or just anything but a u or something else?

Was it helpful?

Solution

(?!u) is a zero-width assertion which means that it matches a position between characters, just like \b matches the position between a word character and a non-word character, or ^ matches the position at the beginning of the string.

In this case, however, it matches any position that isn't before a u, so when it is applied to a string like uuuuxx it will match after the last u and before the first x. It doesn't seem to be a very useful thing to do, but there it is.

OTHER TIPS

If used with the /g modifier, it will move the current search marker to just before a not u character, so further regex tests used with \G will start from there.

One potential more realistic use could be in a split

my $str = 'abcuuuuuduuuu';

my @vals = split /(?!u)/, $str;

use Data::Dump;
dd \@vals;

Outputs:

["a", "b", "cuuuuu", "duuuu"]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top