๐Ÿ“ฐRegex

Regular Expression

Regular expressions, commonly known as regex, are powerful sequences of characters that define a search pattern. In programming, regex serves as a versatile tool for pattern matching within strings. It allows developers to express complex search criteria, enabling the identification and manipulation of text based on specific patterns. With regex, programmers can efficiently perform tasks such as data validation, text parsing, and search and replace operations. Whether it's validating user inputs, extracting information from log files, or manipulating data in a structured manner, regex provides a concise and flexible syntax to streamline these tasks. Its widespread adoption across programming languages makes it an indispensable tool for developers seeking to enhance the efficiency and precision of their string processing tasks.


Check the link below to:

  • Learn

  • Practice

  • Test a regex with your data

  • Or see a better cheat sheet

https://regexlearn.com/


Special Characters

  • .: Wildcard, matches any single character except a newline.

  • *: Quantifier, matches zero or more occurrences of the preceding element.

  • ?: Quantifier (optional), matches zero or one occurrence of the preceding element.

  • ^: Asserts the start of a line.

  • $: Asserts the end of a line.

  • +: Quantifier, matches one or more occurrences of the preceding element.

  • (: Opens a group for capturing and grouping.

  • ): Closes a group initiated by an opening parenthesis.

  • [: Opens a character class, matches any one of the characters within the brackets.

  • ]: Closes a character class initiated by an opening square bracket.

  • \: Escape character, treats the following character literally.

Character Classes

  • \d: Matches any digit (0-9).

  • \D: Matches any non-digit.

  • \w: Matches any word character (alphanumeric + underscore).

  • \W: Matches any non-word character.

  • \s: Matches any whitespace character (space, tab, newline).

  • \S: Matches any non-whitespace character.

Quantifiers

  • {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

  • \b: Word boundary, asserts a position at the beginning or end of a word.

  • \B: Non-word boundary.

Assertions

  • (?=...): Positive lookahead assertion.

  • (?!...): Negative lookahead assertion.

  • (?<=...): Positive lookbehind assertion.

  • (?<!...): Negative lookbehind assertion.

Escape Sequences

  • \n: Newline.

  • \t: Tab.

  • \r: Carriage return.

  • \: Escapes a metacharacter, treats it literally.

Flags

  • g: Global

  • m: Multiline

  • i: Case Insensitive

Example Patterns

  • ^\d{3}-\d{2}-\d{4}$: Matches a social security number.

  • ^([a-zA-Z0-9._%+-]+)@([a-zA-Z0-9.-]+)\.([a-zA-Z]{2,})$: Matches an email address.

  • ^\(\d{3}\) \d{3}-\d{4}$: Matches a phone number in the format (XXX) XXX-XXXX.

  • ^https?://\w+(\.\w+)+(/[\w/.]*)?$: Matches a basic URL.

  • [\w-]{24}\.[\w-]{6}\.[\w-]{27}: Discord token.

  • mfa\.[\w-]{84}: Discord MFA token.

  • ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*()_+])[\w!@#$%^&*()_+]{8,}$: Matches a password with at least 8 characters, including at least one uppercase letter, one lowercase letter, one digit, and one special character.

Last updated