Regular Expressions

Regular expression (or in short regex) is a very useful tool that is used to describe a search pattern for matching the text. Regex is nothing but a sequence of some characters that defines a search pattern. Regex is used for parsing, filtering, validating, and extracting meaningful information from large text, such as logs and output generated from other programs.

Some programmers wonder why they even need to learn regular expressions. Here are some use cases:

  1. 1. Regular expression to extract text between square brackets

  2. 2. Regular expression to extract all Non-Alphanumeric Characters from a String

  3. 3. Regular expression to extract date(YYYY-MM-DD) from string

  4. 4. Regular expression to extract DNS host-name or IP Address from string

  5. 5. Regular expression to extract domain from URL

  6. 6. Regular expression to validate email address

  7. 7. Regular expression to validate phone number

  8. 8. Regular expression to validate the date format in "dd/mm/yyyy"

  9. 9. Regular expression to validate common Credit Card Numbers

  10. 10. Replace any non-alphanumeric character sequences with a dash using Regex

  11. 11. Replace first occurrence of string using Regexp