1

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 
1
  • There are different forms of regular expression. I'm guessing from the content of the string that this is PHP. If so, please add a PHP tag to your question (and you might as well remove the "expression" tag; it doesn't really add anything). Commented Dec 5, 2013 at 19:44

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Bohemian for the explanation.Just wondering how's that ends up replaced as space ($1)?
You can't. Look arounds are non-capturing. It doesn't count as a group at all - ie as far as groups are concerned it's like it us t there.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.