Search code examples
regexphpstorm

Regex to match surrounding text


I need to replace "something[anything]" with "somethingElse(anything)", the tricky part is anything could be anything, and I don't want to replace that. Is it possible to do with regex?

P.S. In real scenario I'd be using PHPStorm to look / replace it. It would help me a lot to show me what to put exactly at the find window.

Thank you!


Solution

  • Idea: Capture 'anything' in a capturing group in regex. Then, back reference it in the replacement.

    For example: search for something[([^]]*)\] and replace with somethingElse($1).