https://github.com/nanoframework/system.text.regularexpressions
:package: .NET nanoFramework System.Text.RegularExpressions Class Library
https://github.com/nanoframework/system.text.regularexpressions
csharp dotnet hacktoberfest library nanoframework parser regex
Last synced: 8 months ago
JSON representation
:package: .NET nanoFramework System.Text.RegularExpressions Class Library
- Host: GitHub
- URL: https://github.com/nanoframework/system.text.regularexpressions
- Owner: nanoframework
- License: mit
- Created: 2021-03-13T13:47:37.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-05-02T12:55:10.000Z (about 1 year ago)
- Last Synced: 2025-10-13T06:25:31.560Z (8 months ago)
- Topics: csharp, dotnet, hacktoberfest, library, nanoframework, parser, regex
- Language: C#
- Homepage: https://www.nanoframework.net
- Size: 297 KB
- Stars: 6
- Watchers: 5
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
[](https://sonarcloud.io/dashboard?id=nanoframework_lib-nanoFramework.System.Text.RegularExpressions) [](https://sonarcloud.io/dashboard?id=nanoframework_lib-nanoFramework.System.Text.RegularExpressions) [](LICENSE) [](https://www.nuget.org/packages/nanoFramework.System.Text/) [](https://github.com/nanoframework/Home/blob/main/CONTRIBUTING.md)
[](https://discord.gg/gCyBu8T)

-----
### Welcome to the .NET **nanoFramework** System.Text.RegularExpressions repository
## Build status
| Component | Build Status | NuGet Package |
|:-|---|---|
| System.Text.RegularExpressions | [](https://dev.azure.com/nanoframework/System.Text.RegularExpressions/_build/latest?definitionId=69&repoName=nanoframework%2FSystem.Text.RegularExpressions&branchName=main) | [](https://www.nuget.org/packages/nanoFramework.System.Text.RegularExpressions/) |
**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 it's 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 next 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!
```
## Validated regular expressions
You'll find in the tests some regular expressions used. Those can be useful:
- email addresses: `([\w\d_.\-]+)@([\d\w\.\-]+)\.([\w\.]{2,5})`
- http(s) URL: `(https?:\/\/)([\da-z-._]+)/?([\/\da-z.-]*)` (limitation: URL has to finish with a / to be properly extracted, this is a bug into our engine, it works perfectly with the expression between ^ and $)
- MD5: `[a-f0-9]{32}`
- SHA256: `[A-Fa-f0-9]{64}`
- Simple XML tag: `[^<]*`
- GUID: `[{]?[0-9a-fA-F]{8}-([0-9a-fA-F]{4}-){3}[0-9a-fA-F]{12}[}]?`
- Date time like `2021-04-10 18:08:42`: `(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})`
## Known limitations
This parser is a simple one, some of those elements are not supported:
- Expressions like `(?\w+)` will not work. While groups are supported, the `?` in front of a named group or element is not supported.
- For some characters, when using the escaped version like `\.` you may encounter issues, just use `.` instead.
- Sometimes the order of the characters may have an impact. If you are in this case, try to change the order in a character class like `[a-z-._]`
## 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).
Join our Discord community [here](https://discord.gg/gCyBu8T).
## Credits
The list of contributors to this project can be found at [CONTRIBUTORS](https://github.com/nanoframework/Home/blob/main/CONTRIBUTORS.md).
## License
The **nanoFramework** Class Libraries are licensed under the [MIT license](LICENSE.md).
Please check the header of the files in this repository, some of the code is under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).
## Code of Conduct
This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community.
For more information see the [.NET Foundation Code of Conduct](https://dotnetfoundation.org/code-of-conduct).
## .NET Foundation
This project is supported by the [.NET Foundation](https://dotnetfoundation.org).