0

I need to find and replace a ton of code that was written and my text editor works with regex expressions but I dont know how to make them. How would I create a regex expression to do this:

from:

if ($q->param('wordOne[]') =~ m/wordTwo/)

to :

if (grep /wordTwo/, $q->param('wordOne[]')) { ... }

wordOne/Two can be any word that is where my problem is and im not sure how to format my regex for a find and then replace to my format

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
BluGeni
  • 3,378
  • 8
  • 36
  • 64
  • 1
    What editor? Their regex engines will be different. The general syntax is often `s/find(this)/replace with \1/g` or similar, but can be something like `find\(this\)` for example (Emacs) – BRPocock Feb 05 '14 at 19:17
  • look at http://stackoverflow.com/questions/11819886/regular-expression-search-replace-in-sublime-text-2 http://stackoverflow.com/questions/17617961/sublime-text-regex-find-and-replace http://stackoverflow.com/questions/11733370/how-do-i-do-a-regex-search-and-replace-in-sublime-text-2 – BRPocock Feb 05 '14 at 19:19

2 Answers2

0

Search for:

if \(\$q->param\('([^']+)'\) =~ m/([^/]+)/\)

([^']+) captures the text in between param(' and ') =~. ([^/]+) is similar. See here for more information on capturing groups.

Replace with:

if (grep /\2/, $q->param('\1')) { ... }

\1 and \2 are backreferences to the captured text.

Output:

if (grep /wordTwo/, $q->param('wordOne[]')) { ... }

Demonstration: http://regex101.com/r/pB2jM1

The Guy with The Hat
  • 10,836
  • 8
  • 57
  • 75
0

Find: (\$q->param\('.*?\[\]'\)) =~ m(/.*?/)

Replace: grep \2, \1

mbroshi
  • 969
  • 8
  • 21