Sep 012014
 

A previous post in this blog explained how to do multiline searches with preg_match in PHP, to search for text strings that span more than one line.

This feature is also available in the vim editor, but the syntax of the regular expressions differs slightly from that used in PHP. This post summarizes these differences, and explains through some examples the right syntax accepted by vim to do a search across multiple lines.

Frequently used regex patterns in vim

A regular expression in vim often includes the following patterns to search text in a single line:

.       any character, except a newline

^     the beginning of the line

$      the end of the line

\s    a space or tab

Examples:

“/abc” Find occurrences of the text “abc” anywhere in the line

“/^abc” Find occurrences of the text “abc” at the beginning of the line

The character “^” only means “beginning of line” if it is the first character in the regex. Analogously, the character “$” only means “end of line” if it is the last character in the regex.

Multiline patterns

The above patterns can be replaced with other patterns that also match the newline character, as follows:

\_.     any character, including a newline

\_^     the beginning of a line

\_$     the end of a line

\_s     a space, tab or newline character

\n       a newline character

Example

The following expression removes all comments (enclosed in “<!–“,  “–>” ) the HTML code of a web page:

Using the above expression on the text:

produces the result:

Note: The regular expression above uses the multiplier “\{-}” to match the shortest string.

If we had used the “*” multiplier:

The regex would have matched everything from the first appearance of “<!–” up to the last appearance of “–>”, and the result would have been:

 

References

 Posted by at 3:09 pm

 Leave a Reply

(required)

(required)