Showing posts with label Regex. Show all posts

How to write regular expressions?

Regular expressions, often abbreviated as regex or regexp, are sequences of characters that define a search pattern. They are incredibly powerful for searching, extracting, and manipulating text based on patterns. Here's a beginner-friendly introduction to regular expressions along with some examples:

1. Basic Matching:

  • .: Matches any single character except newline.
    • Example: c.t matches "cat", "cot", "cut", etc.
  • [ ]: Matches any single character within the brackets.
    • Example: [aeiou] matches any vowel.
  • [^ ]: Matches any single character not within the brackets.
    • Example: [^aeiou] matches any non-vowel.
  • |: Alternation, matches either the expression before or after the |.
    • Example: cat|dog matches "cat" or "dog".