Skip to content

Commit 9ff05d7

Browse files
authored
readd missing sample (#4135)
* readd missing sample * fix lang id
1 parent c8d0f3d commit 9ff05d7

File tree

2 files changed

+31
-0
lines changed
  • samples/snippets/csharp/VS_Snippets_CLR_System/system.Text.RegularExpressions.Regex.Split/cs
  • xml/System.Text.RegularExpressions

2 files changed

+31
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// <Snippet6>
2+
using System;
3+
using System.Text.RegularExpressions;
4+
5+
public class Example
6+
{
7+
public static void Main()
8+
{
9+
string pattern = "(-)";
10+
string input = "apple-apricot-plum-pear-pomegranate-pineapple-peach";
11+
12+
// Split on hyphens from 15th character on
13+
Regex regex = new Regex(pattern);
14+
// Split on hyphens from 15th character on
15+
string[] substrings = regex.Split(input, 4, 15);
16+
foreach (string match in substrings)
17+
{
18+
Console.WriteLine("'{0}'", match);
19+
}
20+
}
21+
}
22+
// The method writes the following to the console:
23+
// 'apple-apricot-plum'
24+
// '-'
25+
// 'pear'
26+
// '-'
27+
// 'pomegranate'
28+
// '-'
29+
// 'pineapple-peach'
30+
// </Snippet6>

xml/System.Text.RegularExpressions/Regex.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4996,6 +4996,7 @@ Allows an <see cref="T:System.Object" /> to attempt to free resources and perfor
49964996
49974997
If capturing parentheses are used in a regular expression, any captured text is included in the array of split strings. However, any array elements that contain captured text are not counted in determining whether the number of matches has reached `count`. For example, splitting the string '"apple-apricot-plum-pear-pomegranate-pineapple-peach" into a maximum of four substrings beginning at character 15 in the string results in a seven-element array, as the following code shows.
49984998
4999+
:::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Text.RegularExpressions.Regex.Split/cs/split6.cs" interactive="try-dotnet" id="Snippet6":::
49995000
:::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Text.RegularExpressions.Regex.Split/vb/split6.vb" id="Snippet6":::
50005001
50015002
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 .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 individual words in a string. The first set of capturing parentheses captures the hyphen, and the second set captures the vertical bar. If the example code is compiled and run under the .NET Framework 1.0 or 1.1, it excludes the vertical bar characters; if it is compiled and run under the .NET Framework 2.0 or later versions, it includes them.

0 commit comments

Comments
 (0)