0

I have the following in a helper extension:

return Regex.Replace(str,
"(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])|(?<=[^0-9])(?<x>[0-9])(?=.)",
 " $1");

Which will effectively take

WhateverIPutInToIt

and render

Whatever I Put In To It

except that I would prefer not to capitalize each proper word. However, if I do not capitalize then it will not render with the spaces.

I thought I would instead try to take something like

Whatever_I_put_into_it

to render what I want. I cannot figure out how to do a regular expression (if its even possible) to strip out the the "underscore" character and replace it with a space.

Any help?

Update: Sorry for not being clear. This is in a helper extension to convert enums I have into something readable. I am using the helper for my dropdownlists and checkboxlists. I have over 10 enums for those, and to do each one individually would be a pain. Was hoping that I could just fix the Regex.Replace code at the top to something that would accomplish what I am after.

Mind you, if I do

YouHave9OrMore

the above Regex.Replace will spit out (it splits out the number "9")

You Have 9 Or More

Thank you all for the quick comments.

REMESQ
  • 1,190
  • 2
  • 26
  • 60
  • I simple replace perhaps instead of a regex or am I missing something? – PeeHaa Feb 13 '12 at 20:46
  • 4
    In this case, you could use `"Whatever_I_put_into_it".Replace('_',' ')`? – porges Feb 13 '12 at 20:47
  • `return Regex.Replace(str,"_"," ");` I really don't see what the problem is. – Feysal Feb 13 '12 at 20:51
  • 1
    So, maybe you don't know which one you get as input? By the way, your pattern is ridiculously complex. – Kobi Feb 13 '12 at 20:52
  • Sorry for not being clear enough as to what I was using. The `Regex.Replace` code at the top of my question is in a helper extension. It's taking any enum values I have for dropdownlists and making them readable. I have lots of enum values, so parsing each one individually is too hard. So, I just wanted something in that helper that would parse all my enums. – REMESQ Feb 13 '12 at 21:05

3 Answers3

2

You shouldn't need a regex for that:

return str.Replace("_"," ");
caleb
  • 2,687
  • 30
  • 25
0

Have you tried your regex with MatchEvaluator parameter?

var finalstr = Regex.Replace(
  "WhateverIPutInToIt", 
  "(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])|(?<=[^0-9])(?<x>[0-9])(?=.)", 
  me => " " + me.Value.ToLower() // <----
);

This would output Whatever i put in to it.

OR

YouHave9OrMore ==> You have 9 or more


If you make it an extension method you can use it like this

var finalstr = System.IO.SearchOption.TopDirectoryOnly.GetString();

public static class MyExtensions
{
    public static string GetString(this Enum en)
    {
        return Regex.Replace(en.ToString(), "(?<=[a-z])(?<x>[A-Z])|(?<=.)(?<x>[A-Z])(?=[a-z])|(?<=[^0-9])(?<x>[0-9])(?=.)", me => " " + me.Value.ToLower());
    }
}
L.B
  • 114,136
  • 19
  • 178
  • 224
  • You're welcome. In fact it's your regex :) I just added `me => " " + me.Value.ToLower()` – L.B Feb 13 '12 at 22:21
0

Are you trying to represent webpage titles within URLs and then convert them back for rendering in a legible form on the page itself? In that case, you should use the HttpUtility.UrlPathEncode and HttpUtility.UrlDecode methods. These would respectively convert "Whatever I Put In To It" to "Whatever%20I%20Put%20In%20To%20It" and back, with the added advantage that other disallowed characters (such as < and >) are also taken care of, and that most browsers would correctly render the encoded sequences in the address bar.

Douglas
  • 53,759
  • 13
  • 140
  • 188
  • No, it's a helper extension that properly displays enums for a checkboxlist or dropdownlist – REMESQ Feb 13 '12 at 21:25