开发者

Regex, how to match multiple lines?

开发者 https://www.devze.com 2023-02-15 11:06 出处:网络
I\'m trying to match the From line all the way to the end of the Subject line in the following: .... From: XXXXXX

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:

/From:.*Date:.*To:.*Subject/m

But that doe开发者_Go百科sn'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: person@example.com
Date: 01-01-2011
To: friend@example.com
Subject: This is the subject line
Random Line 3
Random Line 4
MSG

message.match(/(From:.*Subject.*?)\n/m)[1]
=> "From: person@example.com\nDate: 01-01-2011\nTo: friend@example.com\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:

  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"


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.



Try:

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


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 ;)

0

精彩评论

暂无评论...
验证码 换一张
取 消