[TestMethod] public void ParseVideoFileForDateTime2Test() { var filePath = @"D:\Temp\Nokia 6\Photos\OpenCamera\VID_20190623_085427.mp4"; var fileNameSansExt = Path.GetFileNameWithoutExtension(filePath); var dateTimeFormat = "yyyyMMdd_HHmmss"; var result = string.Empty; Regex regExp = null; CalcRegExpForDateTimeFormat(dateTimeFormat, out regExp); if (regExp != null) { result = regExp.MatchFirstRegExp(fileNameSansExt); } Assert.IsNotNull(regExp); Assert.IsTrue(result.Length == 15); Assert.IsTrue(result == "20190623_085427"); } private static bool CalcRegExpForDateTimeFormat( string dateTimeFormat, out Regex regExp) { var res = false; regExp = null; var newRegex = new StringBuilder(dateTimeFormat.Length); foreach (var ch in dateTimeFormat) { if ((ch == 'y') || (ch == 'M') || (ch == 'd') || (ch == 'H') || (ch == 'm') || (ch == 's')) { newRegex.Append(@"\d"); } else { newRegex.Append(ch); } } var regexStr = newRegex.ToString(); res = (regexStr.Length > 0); if (res) { regExp = new Regex(regexStr, RegexOptions.Compiled); } return res; } public static class RegularExpressionExtensions { /// <summary> /// Match the first occurance of a regular expression in a target string. /// </summary> /// <param name="target">string to search</param> /// <param name="regexp">regular expression object to use for the /// search</param> /// <returns>first match of the regular expression otherwise an /// empty string if there is no match</returns> public static string MatchFirstRegExp(this Regex reg, string target) { var result = ""; MatchCollection mc = reg.Matches(target); if (mc.Count > 0) { result = mc[0].Value; } return result; } }
Showing posts with label regex. Show all posts
Showing posts with label regex. Show all posts
February 9, 2020
Sample Regular Expression
June 9, 2011
Regex Matches Tester
Can find and list matching regular expressions in a string
internal class TestRegexMatches { public void Test() { string typeName = typeof(double).ToString().Replace('.', '_'); string name = "Fudge Factor".Replace(" ", "_"); string plugin = "Smb.Orca.Theo.Samba.Delta, Version=4.20111.99.0, Culture=neutral, PublicKeyToken=19ae4a476ca5c63x"; string[] splitStr = plugin.Split(','); plugin = splitStr[0].Replace('.', '_'); string id = typeName + "\t" + name + "\t" + plugin; int ix=1; MatchCollection matches = Regex.Matches(id, @"[^\t]+"); foreach (Match match in matches) { Console.WriteLine("Match " + ix++.ToString() + ": " + match.Value); } } }produces the output
Match 1: System_Double Match 2: Fudge_Factor Match 3: Smb_Orca_Theo_Samba_Delta
March 1, 2011
Filtering a Multiline string Line by Line using a Regular Expression
For other regular expression stuff see also this blog
This routine filters a string containing multiple lines using the supplied regular expression filter. Set the invert parameter to true if you want the results inverted
This routine filters a string containing multiple lines using the supplied regular expression filter. Set the invert parameter to true if you want the results inverted
private string Filter(string reportText, string regExFilter, bool invert) { if (regExFilter.Length > 0) { StringBuilder output = new StringBuilder(); string[] lines = reportText.Split( new string[] { Environment.NewLine }, StringSplitOptions.None); foreach (string line in lines) { if ((line.Length > 0)) { bool matches = Regex.IsMatch(line, regExFilter, RegexOptions.None); if ((matches && !invert) || (!matches && invert)) output.AppendLine(line); } } return output.ToString(); } else { return reportText; } }
August 16, 2008
Regular Expressions
- For a great step by step tutorial with a test at the end of each step try this web-site: https://regexone.com/
- Regular Expressions Quickstart - This is good for a quick lookup
- Regular Expressions Detailed
- Brief Regex Syntax Guide
- Regular expression tester
? = 0 or 1 match
* = 0..n matches
+ = 1..n matches
{m,n} = from 'm' to 'n' matches where 'm' and 'n' are digits
{n} = exactly 'n' matches where 'n' is a digit
\d = numeric digit
\D = anything but a numeric digit
\s = any whitespace character
\S = any non-whitespace character
\w = Any alphabetical character and '_'
\W = Any non-alphabetical character or '_'
This example matches a United Kingdom postcode:
using System.Text.RegularExpressions; ... const string PostCodeRegEx = @"(GIR0AA|[A-PR-UWYZ]([0-9]{1,2}|" + "([A-HK-Y][0-9]|[A-HK-Y][0-9]([0-9]|[ABEHMNPRV-Y]))|" + "[0-9][A-HJKS-UW])[0-9][ABD-HJLNP-UW-Z]{2})"; public bool IsPostCode(string postCode) { postCode = postCode.Trim().Replace(" ", string.Empty); bool res = Regex.IsMatch(postCode, PostCodeRegEx); return res; }This example Matches a SID:
using System.Text.RegularExpressions; ... result = string.Empty; const string SID_PATTERN = @"(\w-\d-\d-\d{2}-\d+-\d+-\d+)"; Regex reg = new Regex(SID_PATTERN); MatchCollection mc = reg.Matches(sid); if (mc.Count > 0) { result = mc[0].Value; }
Subscribe to:
Posts (Atom)