2017-07-03 80 views
1

我想解析某些地址,我想刪除/ s/o中的/除外的特殊字符| S/O | d/o | D/O | W/O等replaceAll正則表達式排除某些發生

因此,可以說我有一個字符串。就像是。

@Jane Doe. 
W/O John Doe. 
House #250, 
Campbell & Jack ^Street, 
\Random * State, Nowhere. 

我會在

String parsedString = addressString.replaceAll(regex,""); 

使用什麼樣的正則表達式,使parsedString將有輸出。

Jane Doe 
W/O John Doe 
House 250 
Campbell Jack Street 
Random State, Nowhere 

(我代替@,#^ & /(除W/O)。)

+0

赫姆Offtopic:但可能有助於[澄清一些事情...](https://i.stack.imgur.com/TsJyw.jpg) –

+1

你的正則表達式在哪裏? –

+0

可能的重複https://stackoverflow.com/questions/8248277/how-to-determine-if-a-string-has-non-alphanumeric-characters – SomeDude

回答

3

您可以使用此模式與選項不區分大小寫:

String pat = "[@#&^*/.,](?i)(?<![wds]/(?=o))"; 

細節:

[@#&^*/.,] # characters you want to remove 
(?i)  # switch the case insensitive flag 
(?<!  # negative lookbehind: not preceded by 
    [wds]/ # w or d or s and a slash 
    (?=o) # lookahead: followed by a o 
) 
+0

在發佈之前是否測試過它? – Dev

+0

@Dev:沒有爲什麼?.. –

+0

您的更新評論正如我所願。 :) 這是我測試一下,任何人都有興趣。 https://ideone.com/MzWWZW –