Crackermann
Crackermann

Reputation: 65

PHP preg_match_all to match multiple patterns

I have a few 10GB files with mysql info which I would like to filter for a specific table.

The queries look like this (though can have fewer or more line breaks):

SET INSERT_ID=2/\*!\*/;
at 858735202
121124 12:36:53 server id 1  end_log_pos 0  Query   thread_id=9695754   exec_time=0 error_code=0
SET TIMESTAMP=1663753413/\*!\*/;

            INSERT INTO `bank_accounts_daily`
                (
                    `accounts_bank_md5` ,
                    `accounts_bank_payment_desc` ,
                    `accounts_bank_amount` , 
                    `accounts_bank_number` ,
                    `accounts_bank_sortcode` ,
                    `accounts_bank_currency` ,
                    `accounts_bank_date`,
                    `accounts_bank_code`
                )
                VALUES
                (
                    'zxcvxzcvxzc4c9eeca78908296a2f007',
                    'NAMEJO M        1105294            BBP',
                    '278.50',
                    '645450441',
                    '20-55-19',
                    '1',
                    '26/55/2012',
                    'BBP'
                )
/\*!\*/

I am using this, which works to retrieve every single statement:

preg_match_all('/(SET INSERT_ID=([0-9]+)\/\*\!\*\/\;)(.*?)(\/\*\!\*\/\;)(.*?)(\/\*\!\*\/)/s', $input, $output);

But when I attempt to expand it and add the extra pattern to match specifically the 'bank_accounts_daily' pattern it does not retrieve anything(regardless of backticks being escaped or not):

preg_match_all('/(SET INSERT_ID=([0-9]+)\/\*\!\*\/\;)(.*?)(\/\*\!\*\/\;)(.*?)(INSERT INTO \`bank_accounts_daily\`)(.*?)(\/\*\!\*\/)/s', $input, $output);

I do not understand why this is not working. I've tried variations without brackets, but nothing is working. Also - are there any potential problems with my approach that I am not seeing?

Upvotes: 3

Views: 995

Answers (1)

Barmar
Barmar

Reputation: 781059

Try this regexp:

/(SET INSERT_ID=([0-9]+)\/\\\*\!\\\*\/\;)(.*?)(\/\\\*\!\\\*\/\;)(.*?)(INSERT INTO `bank_accounts_daily`)(.*?)(\/\\\*\!\\\*\/)/s

You weren't matching the backslashes in the /\*!\*/ markers. I don't see how the original regexp could have worked, since it also had this mistake.

Upvotes: 2

Related Questions