Link to home
Start Free TrialLog in
Avatar of rmtogether
rmtogether

asked on

C# regular expression question (replace)

Hi, Experts

I would like to add a "space" between number and character for a given string

for example:

March3--> add a "space" between "h" and "3" --> becomes March 3
June 2July 5 --> add a "space" between "2" and "J"--> becomes June 2 July 5

Is it possible? how can I do it. Thanks in advance
Avatar of markoilic
markoilic

try this

string s = Regex.Replace(soureString ,"([a-z]+)(\d{1,2})", "$1\s$2")
Avatar of rmtogether

ASKER

hi, markoillic

I think there is an error in the following 2 parts
"([a-z]+)(\d{1,2})" and  "$1\s$2
Avatar of kaufmed
Regex.Replace(your_string, @"([a-zA-Z])(\d)", @"\1 \2");
Hi, kaufmed:


I try this string "June4July13". looks like not right..
can I add spaces to convert it to "June 4 July 13"?

Thanks
Sorry, I missed the second requirement:

    Regex.Replace(your_string, @"([a-zA_Z])(\d)|(\d)([a-zA-Z])", @"\1 \2");
Hi, kaufmed:

sorry I try your code above, but the result is "Jun\\1 \\2Jul\\1 \\23" not "June 4 July 13"
ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
By "call the function twice" I mean call Regex.Replace() twice.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
@Pui_Yun

Nice  :)
Thank you so much