- UID
- 658062
- 积分
- 2147
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2008-10-22
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
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
|
|