2016-07-27 28 views
0

我想捕捉除字符串以外的所有內容到引號中,有沒有辦法輕鬆做到這一點?如何在我的正則表達式中排除引號中的字符串?

正則表達式是下列之一:包括\b([\w]+)\b|("([^"])*")

這裏有引號的例子:

http://imgur.com/a/MsFNp

+0

你前面的問題來看,我懷疑你需要['(| \ B( \ w +)\ b |「([^」] *)「)'](https://regex101.com/r/eW9vK5/1)。參見[PHP演示](http://ideone.com/QHQ6Qt) –

回答

2

PHP有一個稱爲一個很好的功能*SKIP/*FAIL(名爲回溯控制動詞爲@Federico指出正確):

"[^"]+"(*SKIP)(*FAIL) # everything to the left will be ignored 
|      # or 
\b(\w+)\b    # a word surrounded by boundaries 

a demo on regex101.com


PHP這將是:

$regex = '~"[^"]+"(*SKIP)(*FAIL)|\b(\w+)\b~'; 
$string = 'this one "but this one not" but again this one'; 
preg_match_all($regex, $string, $matches); 
print_r($matches); 
+2

他們被命名爲回溯控制動詞http://www.rexegg.com/backtracking-cont rol-verbs.html –

+0

@FedericoPiazza:更新,謝謝。 – Jan

相關問題