Reputation: 4125
I am trying to expand a CamelCase string this way: CamelCase
>> Camel Case
, using boost.
string Utils::ExpandCamelCase(string & str)
{
static boost::regex camelCaseExpandRegex = boost::regex(R"(\B[A-Z]+?(?=[A-Z][^A-Z])|\B[A-Z]+?(?=[^A-Z]))");
string result = boost::regex_replace(str, camelCaseExpandRegex, " $1");
return result;
}
As you see I am trying to capture first group, which should be the upper letter of each word (excluding first one), and to replace it with a space plus that group.
Something is wrong since "ExpandCamelCasePlease" turns into "Expand amel ase lease".
Trying variations I suspect that I am not capturing the group as I should.
What changes do I need to make to expand correctly camel case inserting spaces before capital letters?
Upvotes: 0
Views: 1197
Reputation: 37377
Use simply (?<=[a-z])(?=[A-Z])
.
It uses lookbehind (?<=[a-z])
, to see if what is behind is lowercase letter and it looks ahead ((?=[A-Z])
) to see if what's ahead it uppercase letter.
Then just replace it with space .
Upvotes: 1
Reputation: 1091
Try this,
string Utils::ExpandCamelCase(string & str)
{
static boost::regex camelCaseExpandRegex = boost::regex(R"([A-Z][^ ]+?)(?=[A-Z]|$)");
string result = boost::regex_replace(str, camelCaseExpandRegex, " $1");
return result;
}
Upvotes: 2