w

Advanced Features

This guide covers advanced features of the Text Replacer tool, including complex regular expressions, capture groups, and sophisticated text manipulation patterns.

Regular Expression Patterns

Character Classes

Word Characters

\w+          # Match one or more word characters (letters, digits, underscore)
\W+          # Match one or more non-word characters

Digit Patterns

\d{3}        # Match exactly 3 digits
\d{3,5}      # Match 3 to 5 digits
\d+          # Match one or more digits

Whitespace Handling

\s+          # Match one or more whitespace characters
\S+          # Match one or more non-whitespace characters

Quantifiers

Basic Quantifiers

a*           # Zero or more 'a' characters
a+           # One or more 'a' characters
a?           # Zero or one 'a' character
a{3}         # Exactly 3 'a' characters
a{3,}        # 3 or more 'a' characters
a{3,6}       # 3 to 6 'a' characters

Greedy vs Lazy

.*           # Greedy: matches as much as possible
.*?          # Lazy: matches as little as possible

Anchors and Boundaries

String Boundaries

^start       # Match 'start' at beginning of string
end$         # Match 'end' at end of string
^complete$   # Match entire string 'complete'

Word Boundaries

\bword\b     # Match 'word' as complete word
\Bword\B     # Match 'word' not at word boundary

Capture Groups

Basic Capture Groups

Simple Grouping

Find:    (\w+)\s+(\w+)
Replace: $2, $1
Input:   John Doe
Result:  Doe, John

Multiple Groups

Find:    (\d{4})-(\d{2})-(\d{2})
Replace: $3/$2/$1
Input:   2023-12-25
Result:  25/12/2023

Named Capture Groups

Named Groups (JavaScript Style)

Find:    (?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})
Replace: ${day}/${month}/${year}
Input:   2023-12-25
Result:  25/12/2023

Non-Capturing Groups

Grouping Without Capturing

Find:    (?:Mr|Mrs|Ms)\.\s+(\w+)
Replace: $1
Input:   Mr. Smith
Result:  Smith

Advanced Patterns

Email Address Extraction

Find:    \b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b
Replace: [EMAIL: $&]

Phone Number Formatting

Find:    (\d{3})(\d{3})(\d{4})
Replace: ($1) $2-$3
Input:   1234567890
Result:  (123) 456-7890

URL Extraction

Find:    https?://[^\s]+
Replace: [LINK: $&]

HTML Tag Removal

Find:    <[^>]+>
Replace: (empty)

Duplicate Line Removal

Find:    ^(.*)$\n\1$
Replace: $1

Lookahead and Lookbehind

Positive Lookahead

Find:    \d+(?=px)
Replace: $&rem
Input:   16px 24px
Result:  16rem 24rem

Negative Lookahead

Find:    \d+(?!px)
Replace: $&px
Input:   16 24
Result:  16px 24px

Positive Lookbehind

Find:    (?<=\$)\d+
Replace: $&.00
Input:   Price: $25
Result:  Price: $25.00

Negative Lookbehind

Find:    (?<!\$)\d+
Replace: $&
Input:   Price: 25
Result:  Price: $25

Complex Replacement Scenarios

Conditional Replacement

Find:    (\d+)(?=\s*dollars?)
Replace: $1 USD
Input:   100 dollars
Result:  100 USD

Multi-line Processing

Find:    ^(.+)$
Replace: - $1
Input:   Line 1
         Line 2
Result:  - Line 1
         - Line 2

Nested Structure Processing

Find:    \{([^}]+)\}
Replace: [BLOCK: $1]
Input:   {nested {content}}
Result:  [BLOCK: nested {content}]

Performance Optimization

Efficient Patterns

  • Use specific character classes instead of .
  • Avoid excessive backtracking with .*
  • Use anchors (^, $) when possible
  • Prefer [^...] over .*? for negation

Large Text Handling

  • Process text in smaller chunks
  • Use global replacement for multiple occurrences
  • Clear history regularly for better performance

Error Handling

Common Regex Errors

# Invalid: Unescaped special characters
Find:    (.*)  # Missing escape for parentheses
Replace: $1

# Valid: Properly escaped
Find:    \(.*\)
Replace: $1

Pattern Validation

  • The tool validates regex patterns in real-time
  • Invalid patterns show error messages
  • Use the preview to test patterns before replacement

Best Practices

Pattern Design

  1. Start Simple: Begin with basic patterns
  2. Test Incrementally: Add complexity gradually
  3. Use Comments: Document complex patterns
  4. Validate Results: Always check output

Replacement Strategy

  1. Backup Data: Keep original text safe
  2. Test on Samples: Use small test cases first
  3. Use History: Save successful patterns
  4. Document Patterns: Note what each pattern does

Real-World Examples

Code Refactoring

# Rename function calls
Find:    oldFunction\(
Replace: newFunction(

Data Cleaning

# Remove extra whitespace
Find:    \s+
Replace: (single space)

Format Conversion

# Convert CSV to Markdown table
Find:    ^(.+),(.+),(.+)$
Replace: | $1 | $2 | $3 |

Ready to try advanced patterns? Open the Text Replacer and experiment with these examples!

Was this page helpful?