Simple (regex?) string manipulation

  • Thread starter Thread starter Marc Gravell
  • Start date Start date
M

Marc Gravell

Blonde moment...

Before I try and do it a harder way, can anybody remind me how to get
the desired from the following? I simply want to split
"aTypicalCamelCasePropertyName" into a "a Typical Camel Case Property
Name" (fully expanding upper-case blocks as the simplest answer to the
ambiguity)

Console.WriteLine(Regex.Replace(
@"testInputOfWhatIWantUPPERS",
@"([a-zA-Z])([A-Z])",
@"$1 $2"));

I want: "test Input Of What I Want U P P E R S"
I currently get: "test Input Of What IWant UP PE RS" - i.e. the upper-
case is getting consumed each time.

Any offers?

Marc
 
Hello Marc,
Blonde moment...

Before I try and do it a harder way, can anybody remind me how to get
the desired from the following? I simply want to split
"aTypicalCamelCasePropertyName" into a "a Typical Camel Case Property
Name" (fully expanding upper-case blocks as the simplest answer to the
ambiguity)

Console.WriteLine(Regex.Replace(
@"testInputOfWhatIWantUPPERS",
@"([a-zA-Z])([A-Z])",
@"$1 $2"));
I want: "test Input Of What I Want U P P E R S"
I currently get: "test Input Of What IWant UP PE RS" - i.e. the upper-
case is getting consumed each time.
Any offers?

Marc

Make the first group a Look behind, so that is is not part of the match.
Then only insert a space before the found character like so:
Console.WriteLine(Regex.Replace(
@"testInputOfWhatIWantUPPERS",
@"(?<=[a-zA-Z])([A-Z])",
@" $1"));
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top