|
| 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 | +} |
0 commit comments