找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 930|回复: 0

[分享] Regex.Split Method (String)

[复制链接]

已领礼包: 859个

财富等级: 财运亨通

发表于 2014-6-9 03:28:47 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
The Regex.Split methods are similar to the String.Split(Char[]) method, except that Regex.Split splits the string at a delimiter determined by a regular expression instead of a set of characters. The string is split as many times as possible. If no delimiter is found, the return value contains one element whose value is the original input string.  
If multiple matches are adjacent to one another, an empty string is inserted into the array. For example, splitting a string on a single hyphen causes the returned array to include an empty string in the position where two adjacent hyphens are found, as the following code shows.
[url=]C#[/url]
VB

Copy            

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);      }   }}// The example displays the following output: //    'plum' //    '' //    'pear'      




If a match is found at the beginning or the end of the input string, an empty string is included at the beginning or the end of the returned array. The following example uses the regular expression pattern \d+ to split an input string on numeric characters. Because the string begins and ends with matching numeric characters, the value of the first and last element of the returned array is String.Empty.
[url=]C#[/url]
VB

Copy            

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();   }}// The example displays the following output: //       '', 'ABCDE', 'FGHIJKL', 'MNOPQ', ''




If capturing parentheses are used in a Regex.Split expression, any captured text is included in the resulting string array. For example, if you split the string "plum-pear" on a hyphen placed within capturing parentheses, the returned array includes a string element that contains the hyphen.
[url=]C#[/url]VB

Copy            

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);      }   }}// The example displays the following output: //    'plum' //    '-' //    'pear'      




However, when the regular expression pattern includes multiple sets of capturing parentheses, the behavior of this method depends on the version of the .NET Framework. In the .NET Framework 1.0 and 1.1, if a match is not found within the first set of capturing parentheses, captured text from additional capturing parentheses is not included in the returned array. Starting with the .NET Framework 2.0, all captured text is also added to the returned array. For example, the following code uses two sets of capturing parentheses to extract the elements of a date, including the date delimiters, from a date string. The first set of capturing parentheses captures the hyphen, and the second set captures the forward slash. If the example code is compiled and run under the .NET Framework 1.0 or 1.1, it excludes the slash characters; if it is compiled and run under the .NET Framework 2.0 or later versions, it includes them.
[url=]C#[/url]VB

Copy            

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))       {         Console.WriteLine("'{0}'", result);      }   }}// Under .NET 1.0 and 1.1, the method returns an array of // 3 elements, as follows: //    '07' //    '14' //    '2007' // // Under .NET 2.0 and later, the method returns an array of // 5 elements, as follows: //    '07' //    '/' //    '14' //    '/' //    '2007'




If the regular expression can match the empty string, Split(String) will split the string into an array of single-character strings because the empty string delimiter can be found at every location. For example:
[url=]C#[/url]VB

Copy            

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);      Console.Write("{");      for(int ctr = 0; ctr < substrings.Length; ctr++)      {         Console.Write(substrings[ctr]);         if (ctr < substrings.Length - 1)            Console.Write(", ");      }      Console.WriteLine("}");   }}// The example displays the following output:    //    {, c, h, a, r, a, c, t, e, r, s, }




Note that the returned array also includes an empty string at the beginning and end of the array.
The RegexMatchTimeoutException exception is thrown if the execution time of the split operation exceeds the time-out interval specified by the Regex.Regex(String, RegexOptions, TimeSpan) constructor. If you do not set a time-out interval when you call the constructor, the exception is thrown if the operation exceeds any time-out value established for the application domain in which the Regex object is created. If no time-out is defined in the Regex constructor call or in the application domain's properties, or if the time-out value is Regex.InfiniteMatchTimeout, no exception is thrown
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-11-17 22:20 , Processed in 0.308440 second(s), 28 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表