简体   繁体   中英

Regex search and replace files in project

I'm trying to change 'class'=>'smart-form' to 'class'=>'smart-form','target'=>'_blank'

I have the following regex to search lines in all my project files matching this criteria (I used regex101 tool to generate the following regex ):

\<\?php \$form = ActiveForm::begin\(\['id' => 'my-form', 'action'=>\['(\w*)\/(\w*)'\], 'options'=>\[('class'=>'smart-form')]]\); \?>

It matches very well when I search lines in my project files with Atom editor

However, I can't use the replace field to replace those lines using this substitution:

$3,'target'=>'_blank'

Is there a package for Atom editor to optimize this task ?

As altenative I could use sed (or perhaps a combination of bash tools provided by MINGW32) But what command I should use?, I'm not experienced using this tool.

Snippet: https://regex101.com/r/nC4hI0/1

Instead of doing the entire string, can you just match on the part that you're trying to replace?

('class'=>'smart-form')

Then your replace, $1,'target'=>'_blank' , would work without removing the rest of the match.

If that's not specific enough, the first solution off the top of my head would be to add captures to the before & after parts, and make your replace like $1$4,'target'=>'_blank'$5 (numbers may need adjustment to be correct).

sed '\#\<?php \$form = ActiveForm::begin(\['id' => 'my-form', 'action'=>\['([[:alnum:]_]*)\/([[:alnum:]_]*)'\], 'options'=>\[('class'=>'smart-form')\]\]); ?># {
   s/'class'=>'smart-form'/&,'target'=>'_blank'/
   }' YourFile
  • posix sed version
  • take your all fingerprint as regex but certainly too big for your real need
  • Assuming the regex matching is a full line (should change if parsed on several line or other info could appear on the same line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM