danneth
danneth

Reputation: 2783

RegExp returning empty string

So, this feels silly, but I can't get my head around it.

I'm trying to catch a id-code and name from a log file, the example boils down to this

$line = "exten => 3002,2,Dial(/dev/null,20); Name Here";
$match = "/exten => ([0-9]{4}),2,Dial\(\/dev\/null,20\); ([\w]*)/Us";

if (preg_match($match, $line, $result))
{
    var_dump($result);  
}

Where I want to grab 3002 and Name Here. The output I get is this

array(3) {
  [0]=>
  string(36) "exten => 3002,2,Dial(/dev/null,20); "
  [1]=>
  string(4) "3002"
  [2]=>
  string(0) ""
}

This feels way to silly to get stuck on, but I'm at a loss, so..I can haz help plz?

Upvotes: 0

Views: 75

Answers (2)

dweeves
dweeves

Reputation: 5605

the problem is surely : ([\w]*) at the end.

\w won't match whitespace , and "Name Here" has whitespaces.

i would recommend ([\w\s]*) for matching "Name Here"

Upvotes: 0

Álvaro González
Álvaro González

Reputation: 146410

My first thought:

...([\w]*)/Us";
        ^  ^
        |  |
        |  \_ Ungreedy
        |
         \_ Zero or more

Edit:

If you just want the rest of the line:

$match = "/exten => ([0-9]{4}),2,Dial\(\/dev\/null,20\); (.+)$/Us";
                                                         ^^^^^

Upvotes: 2

Related Questions