推 ihs:謝謝 我來看看~ 07/22 17:47
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.154.23
> -------------------------------------------------------------------------- <
作者: wawawa (哇哇哇○( ̄﹏ ̄)○) 看板: RegExp
標題: Re: [問題] 不包含連續字串XXX
時間: Wed Jul 2 14:26:21 2008
※ 引述《ihs (新的暱稱)》之銘言:
: 不好意思 找了好久都找不到答案 來問個笨問題
: 例如 PHP 的 preg_match ( $pattern , $subject , $matches )
: 如果要 match $subject 裡不包含某「連續字串」的正規表示式要怎麼寫?
: 像是有兩句話 this is not ok 和 this is ok
: 要濾掉 not 只match > this is ok 這句
: 意思是有點像 /this is !not ok/ 的樣子
: 我知道不包含n,o,t字元是[^not],我想問有排除連續字串這種表示法嗎?
: 希望描述得不會太難懂..
: 謝謝!!
你需要了解的是 lookahead and lookbehind 的概念:
http://www.regular-expressions.info/lookaround.html
以你的範例來說,表示式可以寫成這樣:
/this is (?!not)ok/
這意思是,要比對 this、一個空白、is、一個空白,
後面不能出現 not、最後出現 ok。
說起來可能有點模糊,你可以看看上面這網頁有詳細說明
不然,oreilly 有出一本薄的正規表示式 pocket reference
買回來翻閱也是不錯
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.127.47.125
不好意思 找了好久都找不到答案 來問個笨問題
例如 PHP 的 preg_match ( $pattern , $subject , $matches )
如果要 match $subject 裡不包含某「連續字串」的正規表示式要怎麼寫?
像是有兩句話 this is not ok 和 this is ok
要濾掉 not 只match > this is ok 這句
意思是有點像 /this is !not ok/ 的樣子
我知道不包含n,o,t字元是[^not],我想問有排除連續字串這種表示法嗎?
希望描述得不會太難懂..
謝謝!!
--