C#  regular expression question (replace)

rmtogether
rmtogether used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
try this

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

Author

Commented:
hi, markoillic

I think there is an error in the following 2 parts
"([a-z]+)(\d{1,2})" and  "$1\s$2
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Regex.Replace(your_string, @"([a-zA-Z])(\d)", @"\1 \2");
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Author

Commented:
Hi, kaufmed:


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

Thanks
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Sorry, I missed the second requirement:

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

Author

Commented:
Hi, kaufmed:

sorry I try your code above, but the result is "Jun\\1 \\2Jul\\1 \\23" not "June 4 July 13"
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
So apparently I'm tired...


Disregard the above. Here is a tested version. The only caveat is that you will have to call the function twice for something like "June4July13" because the regex will find "e4"and "y1" and not find "4J" on the first pass. You have to execute it a second rime to pick up the "4J".


    Regex.Replace(your_string, @"[a-zA-Z]\d|\d[a-zA-Z]", AlterMatch);

    \\ AlterMatch function
    string AlterMatch(Match m)
    {
        return string.Concat(m.Value[0], " ", m.Value[1]);
    }
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
By "call the function twice" I mean call Regex.Replace() twice.
Commented:
This will work too (I've tested this, see sample code below) and you'll only need to run this once:

string strResult = System.Text.RegularExpressions.Regex.Replace(strInputText, @"(?ix)((?<=\d)[a-z]|(?<=[a-z])\d)", " $1");

Hope this helps!
P.
string strInputText = "June4July13August5";
string strResult = System.Text.RegularExpressions.Regex.Replace(strInputText, @"(?ix)((?<=\d)[a-z]|(?<=[a-z])\d)", " $1");
MessageBox.Show(strResult);

Open in new window

kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
@Pui_Yun

Nice  :)

Author

Commented:
Thank you so much

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial