我想匹配的是啓動並用相同的報價結尾的字符串,但只有在開始引號和結束:使用lookahead來匹配以引號開頭和結尾並刪除引號的字符串?
"foo bar"
'foo bar'
"the quick brown fox"
但我不希望這些匹配或剝離:
foo "bar"
foo 'bar'
'foo bar"
"the lazy" dogs
我試着用這個Java正則表達式,但它確實不是所有的情況下,相當的工作:
Pattern.compile("^\"|^'(.+)\"$|'$").matcher(quotedString).replaceAll("");
我認爲是有辦法做到超前,但我不知道怎麼牛逼o在這種情況下使用它。
或者設置一個分別檢查它們的if語句會更高效嗎?
Pattern startPattern = Pattern.compile("^\"|^');
Pattern endPattern = Pattern.compile(\"$|'$");
if (startPattern.matcher(s).find() && endPattern.matcher(s).find()) {
...
}
(當然,這將匹配'foo bar"
,我不想)
你是說你想在圍繞兩個或更多單詞s時去除單引號或雙引號在引號內部用空格分開? –
如果字符串在中間有引號會怎麼樣? (例如'這是我的棕色狐狸') –
@MichaelMarkidis - 我並不擔心那個角落案件,這與命令行的東西有關,因此無論如何都是無效的。 – marathon