简体   繁体   中英

Regex Match Part of Line, Then Look Behind and Replace (and also delete that line)

I have a string with this format:

                $something = $gateway->execute($query) or die(mysql_error() . " here");
                $id  = mysql_insert_id();

This is tricky, because I want to end up with this:

$id = $gateway->execute($query) or die(mysql_error() . " here");

I've tried matching the mysql_insert_id line, then looking behind for $gateway->execute and replacing $something with the captured $id, but I coudln't figure it out. Is this even possible? I also want to delete the mysql_insert_id line, but I could always grep and replace afterwards and replace with empty string.

Thanks.

You can match botch elements and reassemble them as needed like that:

\$\w+\s+=\s+(\$gateway->execute\(\$query\)\s+or\s+die\(mysql_error\(\)\s+\.\s+"\s+here"\);).*(\$id)\s+=\s+mysql_insert_id\(\);

Demo

This can be simplified if the $gateway->.. part is a fixed string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM