C# RegEx

Overview

  • Namespace: System.Text.RegularExpressions
  • Classes
    • Capture: Results of a single match
    • CaptureCollection
    • Group: Result of a single group capture
    • Match: Result of a single expression match
    • MatchCollection
    • MatchEvaluator: A delegate for use during replacement operations
    • Regex: An instance of compiled regular expression
      • Escape()
      • IsMatch()
      • Match()
      • Matches()
      • Replace()
      • Split()
      • Unescape()

    Examples

    • Simple matches
      Match m = Regex.Match(
        "abracadabra", "(a|b|r)+");
      if (m.Success)
      {
        log.Debug("Match string: " 
          + m.Value); 
        // Match string: abra
      }
    • String replacement
      Regex.Replace(
      "abracadabra", "abra", "zzzz")
      // zzzzcadzzzz
    • Not alphanumeric: [^a-zA-Z0-9]
    • US zipcode: @”\d{5}([\-]\d{4})?”

References

This entry was posted in c# and tagged , . Bookmark the permalink.