Reputation: 61626
I am trying to convert a Pascal Case string with numbers to a sentence:
OpenHouse2StartTimestamp = > Open House 2 Start Timestamp
I've been able to use regex to separate them without numbers, thanks to this answer, but how to do so when numbers are present is eluding me:
string sentence = Regex.Replace(label, "[a-z][A-Z]", m => m.Value[0] + " " + m.Value[1]);
How can I add numbers into the mix?
Upvotes: 2
Views: 354
Reputation: 626950
You can use
var sentence = Regex.Replace(label, @"(?<=[a-z])(?=[A-Z])|(?<=\d)(?=\D)|(?<=\D)(?=\d)", " ");
See the .NET regex demo. The regex matches:
(?<=[a-z])(?=[A-Z])|
- a location between a lower- and an uppercase ASCII letters, or(?<=\d)(?=\D)|
- a location between a digit and a non-digit, or(?<=\D)(?=\d)
- a location between a non-digit and a digit.Since all you need is inserting a space at the positions matched, you do not need a Match evaluator, just use a string replacement pattern.
Upvotes: 2