I have a nice CamelCase string such as ImageWideNice or ImageNarrowUgly. Now I want to break that string in its substrings, such as Image, Wide or Narrow, and Nice or Ugly.
I thought this could be solved simply by
camelCaseString =~ /(Image)((Wide)|(Narrow))((Nice)|(Ugly))/
But strangely, this will only fill $1 and $2, but not $3.
Do you have a better idea for splitting that string?
?=patternis an example of positive lookahead. It essentially matches a point in the string right before pattern. It doesn’t consume the characters, that is, it doesn’t include pattern as part of the match. Another example:In this case the
sis matched (only the secondtmatches) but not replaced. Thanks to @Bryce and his regexp doc link. Bryce Anderson adds an explanation: