is there a elegant way to parse a word and add spaces before capital letters

后端 未结 7 763
遥遥无期
遥遥无期 2020-12-01 09:43

i need to parse some data and i want to convert

AutomaticTrackingSystem

to

Automatic Tracking System

esse

7条回答
  •  天涯浪人
    2020-12-01 09:59

    You can use lookarounds, e.g:

    string[] tests = {
       "AutomaticTrackingSystem",
       "XMLEditor",
    };
    
    Regex r = new Regex(@"(?!^)(?=[A-Z])");
    foreach (string test in tests) {
       Console.WriteLine(r.Replace(test, " "));
    }
    

    This prints (as seen on ideone.com):

    Automatic Tracking System
    X M L Editor
    

    The regex (?!^)(?=[A-Z]) consists of two assertions:

    • (?!^) - i.e. we're not at the beginning of the string
    • (?=[A-Z]) - i.e. we're just before an uppercase letter

    Related questions

    • How do I convert CamelCase into human-readable names in Java?
    • How does the regular expression (?<=#)[^#]+(?=#) work?

    References

    • regular-expressions.info/Lookarounds

    Splitting the difference

    Here's where using assertions really make a difference, when you have several different rules, and/or you want to Split instead of Replace. This example combines both:

    string[] tests = {
       "AutomaticTrackingSystem",
       "XMLEditor",
       "AnXMLAndXSLT2.0Tool",
    };
    
    Regex r = new Regex(
       @"  (?<=[A-Z])(?=[A-Z][a-z])    # UC before me, UC lc after me
        |  (?<=[^A-Z])(?=[A-Z])        # Not UC before me, UC after me
        |  (?<=[A-Za-z])(?=[^A-Za-z])  # Letter before me, non letter after me
        ",
       RegexOptions.IgnorePatternWhitespace
    );
    foreach (string test in tests) {
       foreach (string part in r.Split(test)) {
          Console.Write("[" + part + "]");
       }
       Console.WriteLine();
    }
    

    This prints (as seen on ideone.com):

    [Automatic][Tracking][System]
    [XML][Editor]
    [An][XML][And][XSLT][2.0][Tool]
    

    Related questions

    • Java split is eating my characters.
      • Has many examples of splitting on zero-width matching assertions

提交回复
热议问题