Naval Navc
Naval Navc

Reputation: 209

c# String - Split Pascal Case

I've been trying to get a C# regex command to turn something like

EYDLessThan5Days

into

EYD Less Than 5 Days

Any ideas?

The code I used :

public static string SplitPascalCase(this string value) {
    Regex NameExpression = new Regex("([A-Z]+(?=$|[A-Z][a-z])|[A-Z]?[a-z0-9]+)",
                                     RegexOptions.Compiled);
    return NameExpression.Replace(value, " $1").Trim();
}

Out:

EYD Less Than5 Days

But still give me wrong result.

Actually I already asked about this in javascript code but when i implemented in c# code with same logic, it's failed.

Please help me. Thanks.

Upvotes: 0

Views: 2691

Answers (3)

Anirudha
Anirudha

Reputation: 32807

Something like this should do

string pattern=@"(?<=\d)(?=[a-zA-Z])|(?<=[a-zA-Z])(?=\d)|(?=[A-Z][a-z])|(?<=[a-z])(?=[A-Z])";
Regex.Replace(input,pattern," ");

Upvotes: 0

Amit Joki
Amit Joki

Reputation: 59252

You can just match and join..

var arr = Regex.Matches(str, @"[A-Z]+(?=[A-Z][a-z]+)|\d|[A-Z][a-z]+").Cast<Match>().Select(m => m.Value).ToArray();
Console.WriteLine(String.Join(" ",arr));

The regex isn't complex at all, it is just capturing each and joining them with a " "

DEMO

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174736

Use lookarounds in your regex so that it won't consume any characters and it allows overlapping of matches.

(?<=[A-Za-z])(?=[A-Z][a-z])|(?<=[a-z0-9])(?=[0-9]?[A-Z])

Replace the matched boundaries with a space.

Regex.Replace(yourString, @"(?<=[A-Za-z])(?=[A-Z][a-z])|(?<=[a-z0-9])(?=[0-9]?[A-Z])", " ");

DEMO

Explanation:

  • (?<=[A-Za-z])(?=[A-Z][a-z]) Matches the boundary which was exists inbetween an upper or lowercase letter and an Uppercase letter which was immediately followed by a lowercase letter. For example. consider this ABc string. And this regex would match, the boundary exists inbetween A and Bc. For this aBc example , this regex would match, the boundary exists inbetween a and Bc

  • | Logical OR operator.

  • (?<=[a-z0-9])(?=[0-9]?[A-Z]) Matches the boundary which was exists inbetween an lower case letter or digit and an optional digit which was immediately followed by an Uppercase letter. For example. consider this a9A string. And this regex would match, the boundary exists inbetween a and 9A , and also the boundary exists inbetween 9 and A, because we gave [0-9] as optional in positive lookahead.

Upvotes: 5

Related Questions