Adding simple example in documentation

This commit is contained in:
Laurent Ellerbach 2021-03-13 18:10:11 +03:00
Родитель 6e0b179bce
Коммит 6743a5738e
2 изменённых файлов: 91 добавлений и 2 удалений

Просмотреть файл

@ -14,6 +14,97 @@
| nanoFramework.System.Text.RegularExpressions | [![Build Status](https://dev.azure.com/nanoframework/nanoFramework.System.Text.RegularExpressions/_apis/build/status/nanoframework.lib-nanoFramework.System.Text.RegularExpressions?branchName=master)](https://dev.azure.com/nanoframework/nanoFramework.System.Text.RegularExpressions/_build/latest?definitionId=57&branchName=master) | [![NuGet](https://img.shields.io/nuget/v/nanoFramework.System.Text.RegularExpressions.svg?label=NuGet&style=flat&logo=nuget)](https://www.nuget.org/packages/nanoFramework.System.Text.RegularExpressions/) |
| nanoFramework.System.Text.RegularExpressions (preview) | [![Build Status](https://dev.azure.com/nanoframework/nanoFramework.System.Text.RegularExpressions/_apis/build/status/nanoframework.lib-nanoFramework.System.Text.RegularExpressions?branchName=develop)](https://dev.azure.com/nanoframework/nanoFramework.System.Text.RegularExpressions/_build/latest?definitionId=57&branchName=develop) | [![](https://badgen.net/badge/NuGet/preview/D7B023?icon=https://simpleicons.now.sh/azuredevops/fff)](https://dev.azure.com/nanoframework/feed/_packaging?_a=package&feed=sandbox&package=nanoFramework.nanoFramework.System.Text.RegularExpressions&protocolType=NuGet&view=overview) |
**Important**: This Regular Expressions parser will cover most of your needs. It has some limitation when the pattern is complex and not a full compatibility. This is an on going work, mainly built on the .NET Microframework implementation. Please do not hesitate to raise any issue if any issue. Also, any help to improve this parser is more than welcome.
In the [Tests](./Tests) you will find advance tests, so far only one is failing. Help to fix the parser needed!
## Usage
The level of compatibility with the full framework is high. The `Match`, `Group` classes are working as you can expect. The following examples gives an idea of the usage:
```csharp
// The example displays the following output:
// Match: This is one sentence.
// Group 1: 'This is one sentence.'
// Capture 1: 'This is one sentence.'
// Group 2: 'sentence'
// Capture 1: 'This '
// Capture 2: 'is '
// Capture 3: 'one '
// Capture 4: 'sentence'
// Group 3: 'sentence'
// Capture 1: 'This'
// Capture 2: 'is'
// Capture 3: 'one'
// Capture 4: 'sentence'
string pattern = @"(\b(\w+?)[,:;]?\s?)+[?.!]";
string input = "This is one sentence. This is a second sentence.";
Match match = Regex.Match(input, pattern);
Debug.WriteLine("Match: " + match.Value);
int groupCtr = 0;
foreach (Group group in match.Groups)
{
groupCtr++;
Debug.WriteLine(" Group " + groupCtr + ": '" + group.Value + "'");
int captureCtr = 0;
foreach (Capture capture in group.Captures)
{
captureCtr++;
Debug.WriteLine(" Capture " + captureCtr + ": '" + capture.Value + "'");
}
}
```
Another example using `Split`:
```csharp
regex = new Regex("[ab]+");
acutalResults = regex.Split("xyzzyababbayyzabbbab123");
for (int i = 0; i < acutalResults.Length; i++)
{
Debug.WriteLine($"{acutalResults[i]}");
}
// The results will be:
// xyzzy
// yyz
// 123
```
You can as well use the `Replace` function:
```csharp
regex = new Regex("a*b");
actual = regex.Replace("aaaabfooaaabgarplyaaabwackyb", "-");
Debug.WriteLine($"{actual}");
regex = new Regex("([a-b]+?)([c-d]+)");
actual = regex.Replace("zzabcdzz", "$1-$2");
Debug.WriteLine($"{actual}");
// The result will be:
// -foo-garply-wacky-
// zzab-cdzz
```
The nex example shows the possibility to use options:
```csharp
regex = new Regex("abc(\\w*)");
Debug.WriteLine("RegexOptions.IgnoreCase abc(\\w*)");
regex.Options = RegexOptions.IgnoreCase;
if (regex.IsMatch("abcddd"))
{
Debug.WriteLine("abcddd = true");
}
regex = new Regex("^abc$", RegexOptions.Multiline);
if (regex.IsMatch("\nabc"))
{
Debug.WriteLine("abc found!");
}
// The result will be:
// abcddd = true
// abc found!
```
## Feedback and documentation
For documentation, providing feedback, issues and finding out how to contribute please refer to the [Home repo](https://github.com/nanoframework/Home).

Просмотреть файл

@ -17,8 +17,6 @@ namespace NFUnitTestRegex
[TestMethod]
public void RegExpTest_1_Split_Test_0()
{
bool testResult = false;
string[] expectedResults;
string[] acutalResults;