好吧,這裏是我的測試(這不是生產代碼,而只是一個測試來說明我的問題)如何用Perl正則表達式替換字符串中的最後一個匹配項?
my $string = <<EOS; # auto generated query
SELECT
users.*
, roles.label AS role_label
, hr_orders.position_label
, deps.label AS dep_label
, top_deps.label AS top_dep_label
FROM
users
LEFT JOIN $conf->{systables}->{roles} AS roles ON users.id_role = roles.id
LEFT JOIN (
SELECT
id_user
, MAX(dt) AS max_dt
FROM
hr_orders
WHERE
fake = 0
AND
IFNULL(position_label, ' ') <> ' '
GROUP BY id_user
) last_hr_orders ON last_hr_orders.id_user = users.id
LEFT JOIN hr_orders ON hr_orders.id_user = last_hr_orders.id_user AND hr_orders.dt = last_hr_orders.max_dt
$join
WHERE
$filter
ORDER BY
$order
$limit
EOS
my $where = "WHERE\nusers.fake = -1 AND ";
$string =~ s{where}{$where}i;
print "result: \n$string";
代碼,生成查詢,簡單的s結尾{哪裏} {$哪裏}我,它取代了每次發生的地方。
我想用'WHERE users.fake = -1'(實際上,用更復雜的模式,但沒關係)替換頂層的WHERE(最後發生的WHERE?)。
任何想法?
+1良好的正則表達式的問題 - 是否有可能,以及如何找到一個字符串的最後出現。 – 2010-10-08 14:23:56