當前位置: 首頁>>代碼示例>>C#>>正文


C# Regex.Split方法代碼示例

本文整理匯總了C#中System.Text.RegularExpressions.Regex.Split方法的典型用法代碼示例。如果您正苦於以下問題:C# Regex.Split方法的具體用法?C# Regex.Split怎麽用?C# Regex.Split使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Text.RegularExpressions.Regex的用法示例。


在下文中一共展示了Regex.Split方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "[a-z]+";
      string input = "Abc1234Def5678Ghi9012Jklm";
      string[] result = Regex.Split(input, pattern, 
                                    RegexOptions.IgnoreCase,
                                    TimeSpan.FromMilliseconds(500));
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:21,代碼來源:Regex.Split

輸出:

'', '1234', '5678', '9012', ''

示例2: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "plum-pear";
      string pattern = "(-)";

      string[] substrings = Regex.Split(input, pattern);    // Split on hyphens
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:18,代碼來源:Regex.Split

輸出:

'plum'
'-'
'pear'

示例3: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = @"07/14/2007";   
      string pattern = @"(-)|(/)";

      foreach (string result in Regex.Split(input, pattern)) 
      {
         Console.WriteLine("'{0}'", result);
      }
   }
}
// In .NET 1.0 and 1.1, the method returns an array of
// 3 elements, as follows:
//    '07'
//    '14'
//    '2007'
//
// In .NET 2.0 and later, the method returns an array of
// 5 elements, as follows:
//    '07'
//    '/'
//    '14'
//    '/'
//    '2007'
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:30,代碼來源:Regex.Split

示例4: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "[a-z]+";
      string input = "Abc1234Def5678Ghi9012Jklm";
      string[] result = Regex.Split(input, pattern, 
                                    RegexOptions.IgnoreCase);
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:20,代碼來源:Regex.Split

輸出:

'', '1234', '5678', '9012', ''

示例5: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\d+";
      Regex rgx = new Regex(pattern);
      string input = "123ABCDE456FGHIJ789KLMNO012PQRST";
      Match m = rgx.Match(input);
      if (m.Success) { 
         int startAt = m.Index;
         string[] result = rgx.Split(input, 3, startAt);
         for (int ctr = 0; ctr < result.Length; ctr++) {
            Console.Write("'{0}'", result[ctr]);
            if (ctr < result.Length - 1)
               Console.Write(", ");
         }
         Console.WriteLine();
      }
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:24,代碼來源:Regex.Split

輸出:

'', 'ABCDE', 'FGHIJKL789MNOPQ012'

示例6: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "(-)|([|])";     // possible delimiters found in string
      string input = "apple|apricot|plum|pear|pomegranate|pineapple|peach";

      Regex regex = new Regex(pattern);    
      // Split on delimiters from 15th character on
      string[] substrings = regex.Split(input, 4, 15);
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
// In .NET 2.0 and later, the method returns an array of
// 7 elements, as follows:
//    apple|apricot|plum'
//    '|'
//    'pear'
//    '|'
//    'pomegranate'
//    '|'
//    'pineapple|peach'
// In .NET 1.0 and 1.1, the method returns an array of
// 4 elements, as follows:
//    'apple|apricot|plum'
//    'pear'
//    'pomegranate'
//    'pineapple|peach'
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:35,代碼來源:Regex.Split

示例7: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "characters";
      Regex regex = new Regex("");
      string[] substrings = regex.Split(input, input.Length, input.IndexOf("a"));
      Console.Write("{");
      for(int ctr = 0; ctr < substrings.Length; ctr++)
      {
         Console.Write(substrings[ctr]);
         if (ctr < substrings.Length - 1)
            Console.Write(", ");
      }
      Console.WriteLine("}");
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:21,代碼來源:Regex.Split

輸出:

{, c, h, a, r, a, c, t, e, rs}

示例8: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\d+";
      Regex rgx = new Regex(pattern);
      string input = "123ABCDE456FGHIJKL789MNOPQ012";
      
      string[] result = rgx.Split(input, 3);
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:21,代碼來源:Regex.Split

輸出:

'', 'ABCDE', 'FGHIJKL789MNOPQ012'

示例9: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = "(-)";
      string input = "apple-apricot-plum-pear-banana";
      Regex regex = new Regex(pattern);         // Split on hyphens.
      string[] substrings = regex.Split(input, 4);
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:18,代碼來源:Regex.Split

輸出:

'apple'
'-'
'apricot'
'-'
'plum'
'-'
'pear-banana'

示例10: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = @"07/14/2007";   
      string pattern = @"(-)|(/)";
      Regex regex = new Regex(pattern);
      foreach (string result in regex.Split(input, 2)) 
      {
         Console.WriteLine("'{0}'", result);
      }
   }
}
// Under .NET 1.0 and 1.1, the method returns an array of
// 2 elements, as follows:
//    '07'
//    '14/2007'
//
// Under .NET 2.0 and later, the method returns an array of
// 3 elements, as follows:
//    '07'
//    '/'
//    '14/2007'
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:27,代碼來源:Regex.Split

示例11: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      Regex regex = new Regex("-");         // Split on hyphens.
      string[] substrings = regex.Split("plum--pear");
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:16,代碼來源:Regex.Split

輸出:

'plum'
''
'pear'

示例12: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\d+";
      Regex rgx = new Regex(pattern);
      string input = "123ABCDE456FGHIJKL789MNOPQ012";
      
      string[] result = rgx.Split(input);
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:21,代碼來源:Regex.Split

輸出:

'', 'ABCDE', 'FGHIJKL', 'MNOPQ', ''

示例13: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string pattern = @"\d+";
      string input = "123ABCDE456FGHIJKL789MNOPQ012";
      string[] result = Regex.Split(input, pattern);
      for (int ctr = 0; ctr < result.Length; ctr++) {
         Console.Write("'{0}'", result[ctr]);
         if (ctr < result.Length - 1) 
            Console.Write(", ");
      }
      Console.WriteLine();
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:19,代碼來源:Regex.Split

輸出:

'', 'ABCDE', 'FGHIJKL', 'MNOPQ', ''

示例14: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      string input = "characters";
      string[] substrings = Regex.Split(input, "");
      Console.Write("{");
      for(int ctr = 0; ctr < substrings.Length; ctr++)
      {
         Console.Write("'{0}'", substrings[ctr]);
         if (ctr < substrings.Length - 1)
            Console.Write(", ");
      }
      Console.WriteLine("}");
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:20,代碼來源:Regex.Split

輸出:

{'', 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's', ''}

示例15: Main

//引入命名空間
using System;
using System.Text.RegularExpressions;

public class Example
{
   public static void Main()
   {
      Regex regex = new Regex("(-)");         // Split on hyphens.
      string[] substrings = regex.Split("plum-pear");
      foreach (string match in substrings)
      {
         Console.WriteLine("'{0}'", match);
      }
   }
}
開發者ID:.NET開發者,項目名稱:System.Text.RegularExpressions,代碼行數:16,代碼來源:Regex.Split

輸出:

'plum'
'-'
'pear'


注:本文中的System.Text.RegularExpressions.Regex.Split方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。