Regex Cheatsheet

easewithtuts

V Sai Harsha

Posted on September 8, 2023

Regex Cheatsheet

Regular expressions, often abbreviated as regex or regexp, provide a powerful and flexible way to search, match, and manipulate text. They consist of a sequence of characters that define a search pattern. This cheat sheet provides a quick reference to common regular expression syntax and usage.

Basic Syntax

  • Literal Characters: Characters match themselves. For example, a matches the character 'a'.
  • Metacharacters: Special characters with special meanings. Examples: ., *, +, ?, \, (), [], {}, |.

Character Classes

  • [abc]: Matches any one of the characters 'a', 'b', or 'c'.
  • [^abc]: Matches any character except 'a', 'b', or 'c'.
  • [a-z]: Matches any lowercase letter.
  • [A-Z]: Matches any uppercase letter.
  • [0-9]: Matches any digit.
  • .: Matches any single character except a newline.

Quantifiers

  • *: Matches 0 or more occurrences of the preceding element.
  • +: Matches 1 or more occurrences of the preceding element.
  • ?: Matches 0 or 1 occurrence of the preceding element.
  • {n}: Matches exactly n occurrences of the preceding element.
  • {n,}: Matches n or more occurrences of the preceding element.
  • {n,m}: Matches between n and m occurrences of the preceding element.

Anchors

  • ^: Matches the start of a line.
  • $: Matches the end of a line.
  • \b: Matches a word boundary.
  • \B: Matches a non-word boundary.

Groups and Alternation

  • (): Groups elements together. For example, (abc)+ matches one or more 'abc' sequences.
  • |: Alternation, matches either the left or right side. For example, cat|dog matches 'cat' or 'dog'.

Escaping

  • \: Escapes a metacharacter to match it as a literal character. For example, \. matches a period.

Predefined Character Classes

  • \d: Matches a digit (equivalent to [0-9]).
  • \D: Matches a non-digit (equivalent to [^0-9]).
  • \w: Matches a word character (equivalent to [a-zA-Z0-9_]).
  • \W: Matches a non-word character (equivalent to [^a-zA-Z0-9_]).
  • \s: Matches a whitespace character (spaces, tabs, newlines, etc.).
  • \S: Matches a non-whitespace character.

Flags (Modifiers)

  • i: Case-insensitive matching.
  • g: Global matching (matches all occurrences, not just the first).
  • m: Multi-line matching (^ and $ match the start/end of each line).

Examples

  • \d{3}-\d{2}-\d{4}: Matches a social security number in the format '123-45-6789'.
  • ^[A-Z][a-zA-Z]*$: Matches a valid name starting with an uppercase letter.
  • (\d{2}-\d{2}){3}: Matches a date in the format 'dd-dd-dd'.
  • https?://[^\s]+: Matches a URL starting with 'http://' or 'https://'.

Conclusion

Regular expressions are a powerful tool for text pattern matching and manipulation. While this cheat sheet covers many common regex elements, there are more advanced features and techniques available. Experiment and practice with regex to become proficient in using this valuable tool for text processing tasks.

💖 💪 🙅 🚩
easewithtuts
V Sai Harsha

Posted on September 8, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related