Search code examples
regexphpstorm

Regex - Match multi-line content between double curly brackets


I am am trying to refactor some code and need to use regular expression to find a large number of strings. An example string is like:

  {{ Form::text('twitter', Input::old('twitter'), 
              array(
                    'class'=>'form-control ',
                    'placeholder'=>'E.g http://www.twitter.com/MyTwitterPage'
                    ))

 }}

I have managed to use \{\{(.*\s*Form::.*\s*)\}\} to match strings when they're on a single line, but it fails to match multi-line strings such as the above.

Also, I'm using PHPStorm's regex find feature if that's of any help.

Any help is much appreciated.


Solution

  • You can use

    \{\{(\s*Form::\w*\((?:[^}]*(?:}[^}]+)*))}}
    

    See the regex demo

    It is basically the same as \{\{(\s*Form::\w*\([\s\S]*?)}}, but it uses an unrolled logic and is thus much effecient.