Skip to content

Latest commit

 

History

History
29 lines (22 loc) · 1.11 KB

dos_and_donts.md

File metadata and controls

29 lines (22 loc) · 1.11 KB

Regular expression strategies

Know what regular expressions (regexes) are good for, and what they don't do well at all.

Use regexes for:

  • Matching text against a pattern
  • Substituting strings when they match a pattern (find and replace)
  • Validating attributes against a pattern
    • The best way to validate an email address beyond idiot-checking is to send mail to it and confirm receipt.
  • Parsing data such as log files with defined delimiters or separators. Some exceptions are listed below.

Do not use regexes for:

  • Parsing markup such as XML (HTML). Use Nokogiri.
  • Parsing serialization formats such as JSON or YAML. Use JSON or Psych/YAML.
  • Parsing URI's. Use URI and have a look at its regular expressions
  • Parsing comma-separated values (CSV). Use CSV.