Regular expressions (regex) are a powerful tool for pattern matching and text processing. This tutorial will guide you through the basics of regex syntax and provide practical examples of how to use them.

Basic Syntax

Here's a brief overview of some common regex syntax elements:

  • . matches any character except a newline
  • * matches zero or more of the preceding element
  • + matches one or more of the preceding element
  • ? matches zero or one of the preceding element
  • [] defines a character class, matching any single character within the brackets
  • ^ asserts the position at the start of a line
  • $ asserts the position at the end of a line

Example: Matching Email Addresses

Suppose you want to match email addresses in a text. Here's a regex pattern that does that:

\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b

Explanation

  • \b asserts a word boundary, ensuring that the match starts at the beginning of a word
  • [A-Za-z0-9._%+-]+ matches one or more characters that are alphanumeric, dots, underscores, percent signs, plus signs, or hyphens
  • @ matches the at symbol
  • [A-Za-z0-9.-]+ matches one or more characters that are alphanumeric, dots, or hyphens
  • \. matches a literal dot
  • [A-Z|a-z]{2,} matches two or more uppercase or lowercase letters
  • \b asserts a word boundary, ensuring that the match ends at the end of a word

More Examples

Here are some more examples of regex patterns:

  • To match a phone number:
    \b\d{3}[-.]?\d{3}[-.]?\d{4}\b
    
  • To match a URL:
    \bhttps?://[^\s]+(?:/|\b)
    
  • To match a date in the format MM/DD/YYYY:
    \b(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/\d{4}\b
    

Further Reading

For more advanced regex techniques, we recommend checking out the official regex documentation. This site provides a regex tester and a comprehensive reference guide.


Regex Syntax Diagram