Skip to content

Commit 337d4c8

Browse files
committed
Add notest for C# Regular Expression
1 parent 7e1944c commit 337d4c8

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

C#/Regular Expression/Program.cs

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* 1. Create a regex pattern
3+
* 2. Use Match() method find pattern in ProjectDescription
4+
* 3. If Match.Success, remove matched pattern in ProjectDescription using String.Replace() method
5+
*/
6+
7+
8+
using System;
9+
using System.Text.RegularExpressions;
10+
11+
12+
namespace RegexTutorial
13+
{
14+
class Program
15+
{
16+
static void Main(string[] args)
17+
{
18+
Console.WriteLine("C# Regular Expression\n");
19+
20+
// OKAY
21+
//var sampleText = "BA8234732 - Gerol Bado Project";
22+
//var sampleText = "INC8234732 - Gerol Bado Project";
23+
//var sampleText = "LINK034 - Gerol Bado Project";
24+
//var sampleText = "REQ298374892374983274 - Gerol Bado Project";
25+
//var sampleText = "ADMINISTRATION";
26+
//var sampleText = "BakerAsssyt Support";
27+
//var sampleText = "SC8234732 Gerol Bado Project";
28+
//var sampleText = "BA192783 Support";
29+
//var sampleText = "LINK09 Gerol Bado Project";
30+
//var sampleText = "S589374- Gerol Bado Project";
31+
//var sampleText = "S589374 -Gerol Bado Project";
32+
//var sampleText = "NOC Intake System";
33+
//var sampleText = "PDcalendar DLL";
34+
//var sampleText = "00000014 - Gerol Bado Project";
35+
//var sampleText = "Gerol Bado Project";
36+
//var sampleText = "00000015 Gerol Bado Project";
37+
//var sampleText = " 372 BakerWorld to SharePoint 2019";
38+
//var sampleText = "000 - My PBS21";
39+
//var sampleText = "007 - My BitLocker";
40+
//var sampleText = "1234 - Gerol Bado Project";
41+
42+
43+
var sampleText = "Zero pattern!";
44+
//var sampleText = "SC23423 Legal Knowhow Portal";
45+
46+
Console.WriteLine(sampleText);
47+
Console.WriteLine(TrimProjectDescription(sampleText));
48+
49+
}
50+
51+
52+
static string TrimProjectDescription(string projectDescription)
53+
{
54+
var digitPattern = @"\s*\d+"; // i.e. 000, 0000123, ' 372'
55+
var alphanumericPattern = @"([A-Z]+\d+)"; // i.e. S584931, BA378232, LINK014
56+
var dashspacePattern = @"(\s*-*\s*)"; // i.e. ' - ', '- ', ' -', ' '
57+
58+
var pattern = $"^({digitPattern}|{alphanumericPattern}){dashspacePattern}";
59+
var regex = new Regex(pattern);
60+
61+
var match = regex.Match(projectDescription);
62+
63+
if (match.Success)
64+
{
65+
return projectDescription.Replace(match.Value, "");
66+
}
67+
else
68+
{
69+
return projectDescription;
70+
}
71+
72+
}
73+
74+
75+
76+
}
77+
}

C#/Regular Expression/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# C# Regular Expression
2+
- regular expression or regex are pattern matching standard for string parsing and replacement
3+
4+
# Resources
5+
- [Regular Expression in C#](https://www.c-sharpcorner.com/UploadFile/955025/regular-expression-in-C-Sharp/)
6+
- [Regular Expression Language - Quick Reference](https://docs.microsoft.com/en-us/dotnet/standard/base-types/regular-expression-language-quick-reference)

0 commit comments

Comments
 (0)