Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how would I add space before and after a specific word in a string?
eg user inputs 5s5s4>5 if I want to change the string into 5 s 5 s 4 > 5 how would I do achieve using regex?


What I have tried:

I've tried using smatch but i think it was a misguided effort
Posted
Updated 16-Jan-22 5:14am
Comments
OriginalGriff 14-Jan-22 2:02am    
You are going to have to define exactly what you want to do rather more clearly: your example is spacing individual characters, not words - and a regex to do that would be very different from a "genuinely word based" solution.

Use the "Improve question" widget to edit your question and provide better information.
Patrice T 14-Jan-22 2:50am    
What is the result for 5st5s34>5 ?
Richard MacCutchan 14-Jan-22 3:14am    
It would be easier just to copy the characters one at a time into a new string, following each one by a space.
Maciej Los 16-Jan-22 11:12am    
You mean somethig like this: regex101: build, test, and debug regex[^]?

1 solution

Use the following code:
string result = Regex.Replace(yourString, @"(?<=.)(?!$)", " ");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900