Skip to content

Replace Regex ​

Recursively apply a regex find-and-replace across all files in a directory.

Hidden command

This command has $hidden = true and does not appear in php artisan list.

Command Information ​

  • Signature: modularous:replace:regex {path} {pattern} {data} [--d|directory=] [--p|pretend]
  • Alias: mod:replace:regex
  • Category: Module

Arguments ​

ArgumentRequiredDescription
pathYesRoot directory to walk (absolute or relative to base_path())
patternYesPCRE regex pattern to search for (without delimiters — / is added automatically)
dataYesReplacement string (supports $1 back-references)

Options ​

OptionDefaultDescription
--directory= / -d**/*.phpGlob pattern to filter which files are processed
--pretend / -pfalsePreview matched files and diffs without writing any changes

What It Does ​

Delegates to RegexReplacement::run(). Files inside vendor/ or node_modules/ are skipped unless path itself points inside those directories. An invalid regex or non-existent path causes an early error exit.

Examples ​

bash
# Preview matches without writing
php artisan modularous:replace:regex app/Modules "OldNamespace" "NewNamespace" --pretend

# Replace across all PHP files
php artisan modularous:replace:regex app/Modules "OldNamespace" "NewNamespace"

# Restrict to a specific glob pattern
php artisan modularous:replace:regex app "Foo\\\\Bar" "Baz\\\\Qux" --directory="**/*Controller.php"