Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Regex, how do to add one extra space between every word starting with Capital [duplicate]

Tags:

c#

regex

I have a string: CategoryName

I need to add space between Category and Name.

So I have to insert a space before each capital letter.


1 Answers

var input = "CategoryName";
var result = Regex.Replace(input, "([a-z])([A-Z])", @"$1 $2"); //Category Name

UPDATE (this will treat sequence of capital letters as one word)

var input = "SimpleHTTPRequest";
var result = Regex.Replace(input, "([a-z]|[A-Z]{2,})([A-Z])", @"$1 $2");
//Simple HTTP Request
like image 50
Sergey Berezovskiy Avatar answered Dec 23 '25 23:12

Sergey Berezovskiy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!