I’m trying to match the
From line all the way to the end of the
Subject line in the following:
.... From: XXXXXX Date: Tue, 8 Mar 2011 10:52:42 -0800 To: XXXXXXX Subject: XXXXXXX ....
So far I have:
But that doesn’t match to the end of the subject line. I tried adding
$ but that had no effect.
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) => "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”.
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:
- replace all the “\n” with SOME_SPECIAL_TOKEN
- search the regexp, and do other operations…
- restore: replace SOME_SPECIAL_TOKEN with “\n”
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.
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:
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 😉