[問題] 類似SQL的Parser

看板RegExp (正規表示式 Regular Expression)作者 (真理斯金)時間15年前 (2010/05/20 21:10), 編輯推噓0(002)
留言2則, 2人參與, 最新討論串1/2 (看更多)
我的String是一個像SQL的Query,有sub select: select A, B from Table where city in ( select id from Table where name in ( select name from Table where id=J )) 現在用 preg_match("/SELECT (.+) FROM (.+) WHERE (.+) IN \([\s]*(.+)\)$/i", $q, $m) Match的結果是(找到第二個from了): Array ( [0] => select A, B from Table where city in ( select id from Table where name in ( select name from Table where id=J )) [1] => A, B from Table where city in ( select id [2] => Table [3] => name [4] => select name from Table where id=J ) ) 我想要的結果是 Array ( [0] => select A, B from Table where city in ( select id from Table where name i [1] => A, B [2] => Table [3] => name [4] => select id from Table where name in ( select name from Table where id=J ) ) 請問要怎麼做才好?謝謝 -- -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.113.198.98

05/20 22:44, , 1F
改成(.+?)就work了...
05/20 22:44, 1F

05/28 16:52, , 2F
這是 greedy 造成的影響。
05/28 16:52, 2F
文章代碼(AID): #1BzJJ5oA (RegExp)
討論串 (同標題文章)
文章代碼(AID): #1BzJJ5oA (RegExp)