Advertisement
teleias

Even More Regex

Aug 3rd, 2015
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. ///Set arr yourself.
  2. ///Returns a List<List<string>>
  3. ///If you have "A || B", it will return
  4. /// { {A, B} }
  5. ///If you have "A , B" , it will return
  6. /// { {A}, {B} }
  7. ///If you have A || B, C, D || E, it will return
  8. /// { {A, B}, {C}, {D, E} }
  9.  
  10.     List<List<string>> regex2()
  11.     {
  12.         string pattern = @"(?<Relationship>(,)|(\|\|)|)(?<Name>\w+)(\[(?<Class>\w+)\]|)";
  13.         string[] arr =
  14.         {
  15.             "Statis_Flange[Class_1]||Objectivity_Anchor[Class_1]",
  16.             "Targeting_Jammer",
  17.             "Targeting_Jammer,Targeting_Jammer"
  18.         };
  19.         List<List<Comp>> list = new List<List<Comp>>() ;
  20.         foreach(string s in arr)
  21.         {
  22.             bool isOptional;
  23.             foreach(Match m in Regex.Matches(s, pattern))
  24.             {
  25.                 string cClass, cName, cRelationship;
  26.                 cName         = m.Groups["Name"]        .Value;
  27.                 cClass        = m.Groups["Class"]       .Value;
  28.                 cRelationship = m.Groups["Relationship"].Value;
  29.                 Debug.Log("Name: "+cName+" Class:"+cClass+" Relationship:"+cRelationship);
  30.                 Comp c = new Comp{ cName = cName, cClass = cClass } ;
  31.                 if(cRelationship == "||")
  32.                 {
  33.                     list[list.Count-1].Add( c );
  34.                 }
  35.                 else //"," or ""
  36.                 {
  37.                     list.Add(new List<Comp>{ c });
  38.                 }
  39.             }
  40.         }
  41.         return list;
  42.     }
  43.     class Comp
  44.     {
  45.         public string cName;
  46.         public string cClass;
  47.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement