Catalog / Regex & Text Manipulation Cheatsheet
Regex & Text Manipulation Cheatsheet
A comprehensive guide to regular expressions and text manipulation techniques, essential for algorithms and interview preparation.
Regex Fundamentals
Basic Metacharacters
|
Matches any single character except newline. Example: |
|
Matches the beginning of the string. Example: |
|
Matches the end of the string. Example: |
|
Matches 0 or more occurrences of the preceding character or group. Example: |
|
Matches 1 or more occurrences of the preceding character or group. Example: |
|
Matches 0 or 1 occurrence of the preceding character or group. Example: |
|
Matches any single character within the set. Example: |
|
Matches any single character not within the set. Example: |
|
Acts as an “OR” operator, matching either the expression before or after the pipe. Example: |
Quantifiers and Grouping
|
Matches exactly Example: |
|
Matches Example: |
|
Matches between Example: |
|
Groups patterns together, allowing you to apply quantifiers or other operations to the entire group. Example: |
|
Escapes special characters, allowing you to match them literally. Example: |
Character Classes
|
Matches any digit (0-9). Example: |
|
Matches any word character (letters, digits, and underscores). Example: |
|
Matches any whitespace character (space, tab, newline, etc.). Example: |
|
Matches any non-digit character. Example: |
|
Matches any non-word character. Example: |
|
Matches any non-whitespace character. Example: |
Advanced Regex Concepts
Lookarounds (Zero-Width Assertions)
|
Asserts that the pattern is followed by the specified Example: |
|
Asserts that the pattern is not followed by the specified Example: |
|
Asserts that the pattern is preceded by the specified Example: |
|
Asserts that the pattern is not preceded by the specified Example: |
Backreferences
|
Refers to the captured group with the corresponding number. Useful for matching repeated patterns. Example: |
Flags/Modifiers
|
Makes the regex case-insensitive. Example: |
|
Finds all matches rather than stopping after the first. Example: |
|
Treats the string as multiple lines, allowing Example: |
|
Allows the Example: |
Text Manipulation Techniques
String Splitting
Splitting by a delimiter |
Use the Example (Python):
|
Splitting by Regex |
Use regex for more complex splitting scenarios. Example (JavaScript):
|
String Replacement
Basic Replacement |
Replace a substring with another string. Example (Java):
|
Regex Replacement |
Use regex for more powerful replacement operations. Example (C#):
|
Substring Extraction
Using indices |
Extract a portion of a string using start and end indices. Example (C++):
|
Regex-based extraction |
Use regex groups to extract specific parts of a string. Example (Ruby):
|
Regex & Text Manipulation in Algorithms
Palindrome Check
Use regex to preprocess the string by removing non-alphanumeric characters and converting to lowercase. Then, compare the string to its reverse. Example (Python):
|
Validating User Input
Regex is excellent for validating formats such as email addresses, phone numbers, or passwords. Example (JavaScript):
|
Parsing Log Files
Regex can be used to extract relevant information from log files. Example (Python):
|
String Compression/Decompression
Text manipulation techniques can be used in string compression and decompression algorithms, such as Run-Length Encoding (RLE). Example (Python):
|