seoppc seoppc - 1 year ago 44
PHP Question

Regex help to parse bind file

I am trying to parse current added domains from bind zone file...

Sample string

net. 86400 in rrsig nsec 5 1 86400
test-name.net. 86400 in rrsig nsec
testname.net. 86400 in rrsig nsec


I have tried following regex...

preg_match_all("/(\S+)\.\s+86400\s+in\s+rrsig/", $str, $matches);


Returns

Array
(
[0] => Array
(
[0] => net. 86400 in rrsig
[1] => test-name.net. 86400 in rrsig
[2] => testname.net. 86400 in rrsig
)

[1] => Array
(
[0] => net
[1] => test-name.net
[2] => testname.net
)

)


It should only returns domains ending with .net tld maybe.

Answer Source

You have not made it clear what it is exactly that you want but you can try the following.

preg_match_all("/\S+\.(?:net|tld)/", $str, $matches);

Result

Array
(
    [0] => Array
        (
            [0] => test-name.net
            [1] => testname.net
        )

)

Or

$pattern = "/\S+\.(?:net|tld)\.(?:\t|\s)+86400(?:\t|\s)+in(?:\t|\s)+rrsig/";
preg_match_all($pattern, $str, $matches);

Result

Array
(
    [0] => Array
        (
            [0] => test-name.net.  86400   in  rrsig
            [1] => testname.net.   86400   in  rrsig
        )

)

The :? used in both examples makes it so the group is not captured and only matches what is in the(). This will reduce results from preg_match_all so you don't need to weed through the array to get what you want.

EDIT

You can use look ahead (?=) to make sure all of the required content is there and then only select the portion you want.

$pattern = 
    "/(?=\S+\.(?:net|tld)\.(?:\t|\s)+86400(?:\t|\s)+in(?:\t|\s)+rrsig)\S+\.(?:net|tld)/"
    ;
preg_match_all($pattern, $str, $matches);

In the example the required content is:

(?=\S+\.(?:net|tld)\.(?:\t|\s)+86400(?:\t|\s)+in(?:\t|\s)+rrsig)

and the desired content is:

\S+\.(?:net|tld)