Multi-line Regex Searches in PhpStorm

This is a quick tip about doing multi-line regular expression (MODX logoregex) searches in PhpStorm. Even if you don’t use PhpStorm, you might find this helpful if your editor can do regex searches but by default won’t do multi-line searches.


The Problem

Sometimes, you want to use a regular expression that searches for text that might be on more than one line. Without a multi-line search, there’s no way to do it. When you do a regex search in code, you can set it to do a multi-line search with a modifier at the end of the patterh. This is no help, though, when you want to do the search in your editor and there’s no easy way to tell it to include line breaks in the pattern.

There may be a way to tell PhpStorm’s editor to do a multi-line search, but if there is, I haven’t found it.


The Solution

The answer is to use a character class that will cover any end of line character. The easiest pattern for that is [\s\S]. This will include any space character, and any non-space character. In other words, any character at all, including newlines.

Say, for example, that you want to find the strings string1 and string2 in a file but the two strings may not be on the same line. You can use this pattern:

[code language=”php”]

The question mark after [\s\S]+ makes the search “non-greedy,” so it will stop at the first match. If you want all the text between the first occurrence of string1 and the *last* occurrence of string2, you can remove it.


For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or
better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , | Posted under MODX, MODX | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

2 Comments on Multi-line Regex Searches in PhpStorm

Leave a Reply

Your email address will not be published. Required fields are marked *