Use C# regex to convert casing in a string

后端 未结 3 376
再見小時候
再見小時候 2021-01-21 07:19

how can I convert this string:

bKk_035A_paint-House_V003
to
BKK_035a_paint-House_v003

with a regular expression (e.g. Regex.R

3条回答
  •  离开以前
    2021-01-21 08:06

    the Regex doesn't match the first string...

    I assume you want the first 3 chars upper case, and the rest lowercase?

    here's a first pass:

    const string mod = @"^([a-z][a-z0-9]{1,2})(_\d{3}[a-z]{0,2}_[a-z]+_v{1}\d{3,5})$";
    var converted = 
        new Regex(mod, RegexOptions.IgnoreCase)
            .Replace(input1, 
                m => string.Format(
                       "{0}_{1}",
                       m.Groups[1].ToString().ToUpper(),
                       m.Groups[2].ToString().ToLower()
                     )
                    );
    

提交回复
热议问题