Regular Expressions

Scenario:

Various regular expression scenarios

Solution:

  • Hex Character
    1. var rex = new Regex("[0-9,A-F,a-f]{2}");
  • Line Breaks 
    1. var rex = new Regex(@"[\r\n\t]");
  • File name "${RND:###}-${TS:yyyyMMdd}.${RND:#}.csv";
    1. var fileFormat = new Regex(@"\${(?<type>(RANDOM|TIMESTAMP)):(?<format>[^}]+)}",
                  RegexOptions.Compiled | RegexOptions.CultureInvariant | RegexOptions.IgnoreCase);
  • Replace
    1. var regex = new Regex(@"""(\d+\.\d)""", RegexOptions.Compiled);
      
      response = regex.Replace(response, @"""percent$1""");
  • "fieldName[]"
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    using System.Text.RegularExpressions;
    
    namespace Web.Models
    {
        public class Class
        {
            public bool Validate()
            {
                var value = "fieldName[]";
                var regex = new Regex(@"^fieldName\[.*?\]$",
                       RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
    
                if (regex.IsMatch(value))
                {
                    return true;
                }
                return false;
            }
        }
    }

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...