MFIHRI
MFIHRI

Reputation: 307

What does this group (?<!^) mean in regex?

Newbe here, learnt some basics and came across this regular expression. Would be great if someone can help deconstruct it for me. Thank you in advance !

$source = "ExpandCamelCaseAPIDescriptorPHP5_3_4Version3_21Beta";
preg_replace('/(?<!^)([A-Z][a-z]|(?<=[a-z])[^a-z]|(?<=[A-Z])[0-9_])/', ' $1', $source);
// outputs:Expand Camel Case API Descriptor PHP 5_3_4 Version 3_21 Beta

Upvotes: 1

Views: 51

Answers (1)

Bohemian
Bohemian

Reputation: 424993

The expression

(?<!^)

means "not preceded by start of input", or in other words "anywhere other than the start".

It's a negative look behind, which has the form (?<!regex) and is a zero-width assertion that the preceding input does not match regex. Replace the ! with a = and you get a positive look behind. Remove the < from a look behind and you get look aheads.

Upvotes: 4

Related Questions