tags:

views:

52

answers:

3

Hi,

I am grabbing a block of text, within this block there will be a line containing a phrase that ends "WITH PASSWORD kEqHqPUd" where kEqHqPUd is a dynamically generated password. Can anyone provide a simple regex for grabbing just the password within this. FWIW i'm using PHP.

Thanks

+3  A: 
preg_match('/WITH PASSWORD (.*)$/im', $block, $matches);
//result in $matches[1]

The key is the "m" modifier.

Artefacto
+3  A: 

Not sure how you define "phrase that ends"... But if you means "a sentence ends with the password, but could be followed by another sentence", then a solution could be :

$text = <<<TEXT
This is some
text and there is a sentence that ends
with WITH PASSWORD kEqHqPUd. Is that
matched ?
TEXT;

$matches = array();
if (preg_match('/WITH PASSWORD ([\w\d]+)/', $text, $matches)) {
    var_dump($matches[1]);
}

This will only work, though, if the password only contains letters and/or numbers -- but will work if there is something after "the end of the phrase".


If you meant that the password is the last thing in the whole string, you could use something like this :

$text = <<<TEXT
This is some
text and there is a sentence that ends
with WITH PASSWORD kEqHqPUd
TEXT;

$matches = array();
if (preg_match('/WITH PASSWORD (.+)$/m', $text, $matches)) {
    var_dump($matches[1]);
}

Note that the $ sign means "end of string".

Pascal MARTIN
I confess the ambiguity did not occur to me, but I think the capital letters hinted that it was not actually flowing English text with proper sentences.
Artefacto
A: 

Snaken,

Assuming that PHP accepts standard regex patterns the following should work "WITH PASSWORD (.+)+$" where the password will be placed in the frist capture group.

Tedford