Jump to content

Recommended Posts

All,

Say i have the following data:

 

Tue, 13 Oct 2009 10:52:37 -0700 (PDT)

Date: Tue, 13 Oct 2009 13:52:37 -0400

Message-ID:

Subject: Test Subject

From: Test User

 

How can I search the whole data for the Subject: and then read in the subject into a variable until it gets to the new line character?

 

Thanks in advance.

Well if it was the same as your one, it would need to have the ^ removed as you say, and that leaves it open to match again somewhere in the text if it's just part of the subject. With the ^ and the $ it's forcing a full line to be matched only

Well if it was the same as your one, it would need to have the ^ removed as you say, and that leaves it open to match again somewhere in the text if it's just part of the subject. With the ^ and the $ it's forcing a full line to be matched only

 

Jay, you don't need the $, nor make the quantifier lazy (but you are right about the 'm' modifier though, as CV's version of ^ would look for 'Search' at the beginning of the entire string). Reason being that since the OP only wants to match to a newline, and since the dot by default doesn't include this, that is why CV used .* to this advantage.

 

My approach would be a hybrid of yours and CV's, with a dash of \K:

 

$content = <<<EOF
Tue, 13 Oct 2009 10:52:37 -0700 (PDT)
Date: Tue, 13 Oct 2009 13:52:37 -0400
Message-ID:
Subject: Test Subject
From: Test User
EOF;

preg_match('#^Subject: \K.*#m',$content,$match);
echo $match[0];

What exactly does the \k do?

 

You mean \K (cap letter)

You can read up about that here.

 

On a side note related to that article.. Member Salathe has pointed out to me that this (\K) is not an assertion, but rather an escape sequence.. so I'll have to amend that article to reflect this.

 

EDIT - In case you were wondering why the use of \K in this example... it saves on using a capture.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.