A blazing-fast command-line tool for performing regex-based search and replace operations across multiple files recursively, built with Rust for maximum performance and safety.
- Quick Start
- Features
- Installation
- Usage
- Ignore Patterns
- Regular Expression Syntax
- Testing
- Safety Features
- Performance
- Contributing
# Install from crates.io
cargo install regex-replace
# Basic usage
regex-replace -p 'old_text' -r 'new_text'
# Preview changes without modifying files
regex-replace -p 'pattern' -r 'replacement' --dry-run- Recursive file processing - Search through directories and subdirectories
- Regex pattern matching - Full regex support for complex pattern matching
- File extension filtering - Process only specific file types
- Ignore patterns - Skip files/directories using
.rr_ignorefiles (gitignore syntax) - Dry run mode - Preview changes without modifying files
- Verbose output - See detailed information about matches and replacements
- Fast performance - Built with Rust for speed and efficiency
- Safe operation - Graceful error handling for unreadable files
- Hidden files support - Option to include hidden files and directories
# Clone the repository
git clone https://github.com/yourusername/RustFindAndReplace.git
cd RustFindAndReplace
# Build the project
cargo build --release
# The binary will be available at ./target/release/regex-replacecargo install --path .regex-replace [OPTIONS]-p, --pattern <PATTERN>- Regex pattern to search for (required)-r, --replace <REPLACE>- Replacement text (required)-d, --directory <DIRECTORY>- Directory to search in (default: current directory)-e, --extensions <EXTENSIONS>- File extensions to include (comma-separated, e.g., "txt,rs,js")-n, --dry-run- Show what would be changed without modifying files-v, --verbose- Display detailed output including match information--include-hidden- Include hidden files and directories in search-h, --help- Print help information-V, --version- Print version information
Replace all numbers with "XXX" in the current directory:
regex-replace -p '\d+' -r 'XXX'Replace "TODO" with "DONE" in only .rs and .txt files:
regex-replace -p 'TODO' -r 'DONE' -e 'rs,txt'Preview changes without modifying files:
regex-replace -p 'old_function' -r 'new_function' --dry-run --verboseReplace email addresses with "[REDACTED]" in the docs folder:
regex-replace -p '\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b' -r '[REDACTED]' -d ./docsReplace function declarations using capture groups:
regex-replace -p 'fn (\w+)\(' -r 'function $1(' -e 'rs'Include hidden files
Search and replace in all files including hidden ones:
regex-replace -p 'DEBUG' -r 'RELEASE' --include-hiddenThe tool supports .rr_ignore files to exclude files and directories from processing. These files use gitignore-style syntax.
- Project-specific:
.rr_ignorein the current working directory - Target directory:
.rr_ignorein the directory being searched (if different from cwd) - User-global:
~/.rr_ignorein your home directory
All ignore files are combined, with patterns from all files being applied.
*.log- Ignore all .log filestarget/**- Ignore all files in the target directorynode_modules/**- Ignore all files in node_modules.git/**- Ignore all files in .git directory*.{tmp,temp,swp}- Ignore files with these extensions#- Lines starting with # are comments- Empty lines are ignored
# Build artifacts
target/**
*.o
*.so
# Version control
.git/**
.svn/**
# Dependencies
node_modules/**
vendor/**
# IDE files
.vscode/**
.idea/**
*.iml
# Temporary files
*.tmp
*.swp
*~
This tool uses Rust's regex crate, which supports:
- Character classes:
[a-z],\d,\w,\s - Quantifiers:
*,+,?,{n,m} - Anchors:
^,$,\b - Groups:
(...),(?:...),(?P<name>...) - Capture groups in replacements:
$1,$2, etc.
For full regex syntax documentation, see: https://docs.rs/regex/latest/regex/#syntax
The project includes comprehensive unit and integration tests:
# Run all tests
cargo test
# Run tests with output
cargo test -- --nocapture
# Run only unit tests
cargo test --lib
# Run only integration tests
cargo test --test integration_test- Non-destructive by default: Use
--dry-runto preview changes - File validation: Skips binary files and handles encoding errors gracefully
- Atomic writes: Files are written completely or not at all
- Clear error messages: Detailed error reporting for debugging
RustFindAndReplace is optimized for maximum performance:
- Efficient directory traversal with WalkDir
- Regex compilation once per run (not per file)
- Memory-efficient streaming file processing
- Minimal allocations during replacement operations
- Skip binary files automatically for faster processing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Run tests (
cargo test) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
Built with Rust 2021 edition for stability and modern language features.
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with Rust
- Uses regex for pattern matching
- Command-line parsing by clap
- Directory traversal with walkdir
- Error handling via anyhow
- Pattern matching with globset