Duplication possible:
Expression régulière, diviser la chaîne par lettre majuscule mais ignorer TLA
J’ai une chaîne qui est une combinaison de plusieurs mots, chaque mot est en majuscule.
Par exemple: PlusieursWordsSsortingng
En utilisant C #, comment diviser la chaîne en plusieurs chaînes de mots de manière intelligente?
Merci!
Utilisez cette regex (j’ai oublié de quelle réponse stackoverflow je l’ai obtenue, la rechercherai maintenant):
public static ssortingng ToLowercaseNamingConvention(this ssortingng s, bool toLowercase) { if (toLowercase) { var r = new Regex(@" (?<=[AZ])(?=[AZ][az]) | (?<=[^AZ])(?=[AZ]) | (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace); return r.Replace(s, "_").ToLower(); } else return s; }
Je l'utilise dans ce projet: http://www.ienablemuch.com/2010/12/intelligent-brownfield-mapping-system.html
[MODIFIER]
Je l'ai trouvé maintenant: Comment convertir CamelCase en noms lisibles par l'homme en Java?
Joliment divisé "TodayILiveInTheUSAWithSimon", pas d'espace devant "Today":
using System; using System.Text.RegularExpressions; namespace TestSplit { class MainClass { public static void Main (ssortingng[] args) { Console.WriteLine ("Hello World!"); var r = new Regex(@" (?<=[AZ])(?=[AZ][az]) | (?<=[^AZ])(?=[AZ]) | (?<=[A-Za-z])(?=[^A-Za-z])", RegexOptions.IgnorePatternWhitespace); string s = "TodayILiveInTheUSAWithSimon"; Console.WriteLine( "YYY{0}ZZZ", r.Replace(s, " ")); } } }
Sortie:
YYYToday I Live In The USA With SimonZZZ
ssortingng[] SplitCamelCase(ssortingng source) { return Regex.Split(source, @"(?
Échantillon:
Vous pouvez simplement parcourir les caractères et append des espaces là où vous en avez besoin:
ssortingng theSsortingng = "SeveralWordsSsortingng"; SsortingngBuilder builder = new SsortingngBuilder(); foreach (char c in theSsortingng) { if (Char.IsUpper(c) && builder.Length > 0) builder.Append(' '); builder.Append(c); } theSsortingng = builder.ToSsortingng();
public static IEnumerable SplitOnCapitals(ssortingng text) { Regex regex = new Regex(@"\p{Lu}\p{Ll}*"); foreach (Match match in regex.Matches(text)) { yield return match.Value; } }
Cela va gérer Unicode correctement.
ssortingng str1 = "SeveralWordsSsortingng"; ssortingng newssortingng = ""; for (int i = 0; i < str1.Length; i++) { if (char.IsUpper(str1[i])) newstring += " "; newstring += str1[i].ToString(); }
Voici la réponse à votre problème: expression régulière, diviser la chaîne par lettre majuscule mais ignorer TLA