Regex, how to match multiple lines?

Solution 1:

You can use the /m modifier to enable multiline mode (i.e. to allow . to match newlines), and you can use ? to perform non-greedy matching:

message = <<-MSG
Random Line 1
Random Line 2
From: [email protected]
Date: 01-01-2011
To: [email protected]
Subject: This is the subject line
Random Line 3
Random Line 4
MSG

message.match(/(From:.*Subject.*?)\n/m)[1]
=> "From: [email protected]\nDate: 01-01-2011\nTo: [email protected]\nSubject: This is the subject line"

See http://ruby-doc.org/core/Regexp.html and search for "multiline mode" and "greedy by default".

Solution 2:

If you are using ruby, you can try :

Regexp.new("some reg", Regexp::MULTILINE)

If you are not using ruby, I suggest you hack this question:

  1. replace all the "\n" with SOME_SPECIAL_TOKEN
  2. search the regexp, and do other operations...
  3. restore: replace SOME_SPECIAL_TOKEN with "\n"

Solution 3:

If you want to match across linebreaks, one possibility is to first replace all newline characters with some other character (or character sequence) that wouldn't otherwise appear in the text. For example, if you have all of the text in one string variable you can do something like aString.split("\n").join("|") to replace all newlines in the string with pipe characters.

Also, look at Alan Moore's answer to your previous question regarding how to match the newline character in a regular expression.

Solution 4:


Try:

/...^Subject:[^\n]*/m

Solution 5:

Using the following data:

From: XXXXXX
Date: Tue, 8 Mar 2011 10:52:42 -0800
To: XXXXXXX
Subject: XXXXXXX

The following regex will do the magic:

From:([^\r\n]+)[\r\n]+Date:([^\r\n]+)[\r\n]+To:([^\r\n]+)[\r\n]+Subject:([^\r\n]+)[\r\n]+

But I would recommend that you don't try and do this in 1 regex. Push into a regex "^(\w+):(.+)$" line by line, unless you are sure that the sequence of the FROM/DATE/TO/SUBJECT is not going to change ;)