Search Replace
Regex-powered find and replace patterns for text transformation, refactoring, and data reformatting
You are an expert in using regex-based find and replace for text transformation, code refactoring, and data reformatting.
## Key Points
- Always preview replacements before applying. Use your editor's "Replace All" preview or run the regex with a print/log before writing.
- Use word boundaries `\b` when renaming identifiers to avoid partial matches within longer names.
- Test with edge cases: start of line, end of line, empty strings, adjacent matches.
- For large-scale refactoring, combine regex with version control. Make the replacement, review the diff, and revert if incorrect.
- Use function-based replacements for logic that cannot be expressed in a static replacement string (conditional logic, case conversion, lookups).
- Chain multiple simple replacements rather than writing one complex pattern. Readability and correctness improve with simpler steps.
- Forgetting to escape `$` or `\` in replacement strings. In most engines, `$1` is a group reference. A literal `$` needs escaping.
- Using `.*` without the lazy modifier when matching content between delimiters, causing the replacement to span too much text.
- Not using the global flag. Without `g` in JavaScript or `re.sub` count parameter in Python, only the first match is replaced.
- Replacing inside strings or comments unintentionally during code refactoring. Regex has no concept of language syntax. Use AST-based tools for precision refactoring.
- Replacing content that was already replaced in the same pass. Most engines process left-to-right and do not re-scan replaced text, but chained replacements can compound unexpectedly.
- Destroying whitespace or formatting during replacement. Always verify that indentation and line endings are preserved.
## Quick Example
```javascript
'hello world'.replace(/\b\w+/g, match =>
match.charAt(0).toUpperCase() + match.slice(1)
);
// "Hello World"
```
```regex
Search: (\d{2})/(\d{2})/(\d{4})
Replace: $3-$1-$2
```skilldb get regex-skills/Search ReplaceFull skill: 219 linesSearch & Replace — Regular Expressions
You are an expert in using regex-based find and replace for text transformation, code refactoring, and data reformatting.
Core Philosophy
Overview
Search and replace with regex goes beyond simple string substitution. Capture groups, back-references, and conditional replacements enable powerful structural transformations: reformatting dates, renaming variables, restructuring data, and migrating code patterns at scale.
Core Concepts
Replacement String Syntax
Replacement strings reference captured groups:
| Engine | Numbered Group | Named Group |
|---|---|---|
| Python | \1 or \g<1> | \g<name> |
| JavaScript | $1 | $<name> |
| Java | $1 | ${name} |
| PCRE/PHP | $1 or \\1 | ${name} |
| Sed | \1 | N/A |
Function-Based Replacement
When the replacement logic is too complex for a static string, most languages allow a function as the replacement argument.
Python:
import re
def to_title(match):
return match.group(0).title()
result = re.sub(r'\b\w+', to_title, 'hello world')
# "Hello World"
JavaScript:
'hello world'.replace(/\b\w+/g, match =>
match.charAt(0).toUpperCase() + match.slice(1)
);
// "Hello World"
Implementation Patterns
Reformat dates from MM/DD/YYYY to YYYY-MM-DD
Search: (\d{2})/(\d{2})/(\d{4})
Replace: $3-$1-$2
03/17/2026 becomes 2026-03-17
Swap first and last names
Search: (?P<first>\w+)\s+(?P<last>\w+)
Replace: \g<last>, \g<first>
Jane Doe becomes Doe, Jane
Convert camelCase to snake_case
Python:
import re
def camel_to_snake(name):
s1 = re.sub(r'([A-Z]+)([A-Z][a-z])', r'\1_\2', name)
return re.sub(r'([a-z\d])([A-Z])', r'\1_\2', s1).lower()
camel_to_snake('getUserName') # "get_user_name"
camel_to_snake('parseHTTPResponse') # "parse_http_response"
Convert snake_case to camelCase
Python:
import re
def snake_to_camel(name):
return re.sub(r'_([a-z])', lambda m: m.group(1).upper(), name)
snake_to_camel('get_user_name') # "getUserName"
Wrap bare URLs in Markdown links
Search: (?<!\()(https?://\S+)(?!\))
Replace: [$1]($1)
Visit https://example.com today becomes Visit [https://example.com](https://example.com) today
Add thousands separators to numbers
Python:
re.sub(r'(\d)(?=(\d{3})+(?!\d))', r'\1,', '1234567')
# "1,234,567"
Strip HTML tags
Search: <[^>]+>
Replace: (empty string)
<p>Hello <b>world</b></p> becomes Hello world
Normalize whitespace
Search: \s+
Replace: (single space)
Collapses all runs of whitespace (including tabs and newlines) into a single space.
Remove comment lines from a config file
Search: ^\s*#.*$\n?
Replace: (empty string)
With the multiline flag, removes all lines starting with #.
Rename a function across a codebase
Search: \bgetUserData\b
Replace: fetchUserProfile
The word boundaries ensure getUserDataById is not affected.
Convert print statements to logging calls (Python 2 to 3 style)
Search: print\s+"([^"]*)"
Replace: print("\1")
Convert string concatenation to template literals (JavaScript)
Search: "([^"]*)" \+ (\w+) \+ "([^"]*)"
Replace: `$1${$2}$3`
"Hello " + name + "!" becomes `Hello ${name}!`
Bulk update import paths
Search: from\s+components/(\w+)
Replace: from @/components/$1
Redact sensitive data
Replace credit card numbers with masked versions:
Search: \b(\d{4})\d{8}(\d{4})\b
Replace: $1********$2
4111111111111111 becomes 4111********1111
Best Practices
- Always preview replacements before applying. Use your editor's "Replace All" preview or run the regex with a print/log before writing.
- Use word boundaries
\bwhen renaming identifiers to avoid partial matches within longer names. - Test with edge cases: start of line, end of line, empty strings, adjacent matches.
- For large-scale refactoring, combine regex with version control. Make the replacement, review the diff, and revert if incorrect.
- Use function-based replacements for logic that cannot be expressed in a static replacement string (conditional logic, case conversion, lookups).
- Chain multiple simple replacements rather than writing one complex pattern. Readability and correctness improve with simpler steps.
Common Pitfalls
- Forgetting to escape
$or\in replacement strings. In most engines,$1is a group reference. A literal$needs escaping. - Using
.*without the lazy modifier when matching content between delimiters, causing the replacement to span too much text. - Not using the global flag. Without
gin JavaScript orre.subcount parameter in Python, only the first match is replaced. - Replacing inside strings or comments unintentionally during code refactoring. Regex has no concept of language syntax. Use AST-based tools for precision refactoring.
- Replacing content that was already replaced in the same pass. Most engines process left-to-right and do not re-scan replaced text, but chained replacements can compound unexpectedly.
- Destroying whitespace or formatting during replacement. Always verify that indentation and line endings are preserved.
Anti-Patterns
Over-engineering for hypothetical scale. Building for millions of users when you have hundreds adds complexity without value. Solve today's problems first.
Ignoring the existing ecosystem. Reinventing functionality that mature libraries already provide well wastes time and introduces unnecessary risk.
Premature abstraction. Creating elaborate frameworks and utilities before you have enough concrete cases to know what the abstraction should look like produces the wrong abstraction.
Neglecting error handling at boundaries. Internal code can trust its inputs, but system boundaries (user input, APIs, file I/O) require defensive validation.
Skipping documentation for obvious code. What is obvious to you today will not be obvious to your colleague next month or to you next year.
Install this skill directly: skilldb add regex-skills
Related Skills
Basics Syntax
Core regular expression syntax including character classes, quantifiers, anchors, and alternation
Email URL Validation
Practical regex patterns for validating emails, URLs, IP addresses, and other common string formats
Log Parsing
Regex patterns for parsing structured and semi-structured log files from common servers, applications, and systems
Lookahead Lookbehind
Lookahead and lookbehind assertions for matching patterns based on surrounding context without consuming characters
Named Groups
Named capture groups for readable, maintainable regex patterns with structured data extraction
Performance
Regex performance optimization, catastrophic backtracking prevention, and engine internals for writing efficient patterns