2016-09-25 177 views
-4

我有一個字符串 - +3]##i recently purchased the canon powershot g3 and am extremely satisfied with the purchase . use正則表達式的字符串分割字符串成部分

我想(blankspace/whitespace)

輸出更換+3]##
i recently purchased the canon powershot g3 and am extremely satisfied with the purchase . use

有多次出現的+3]##和數字不斷變化它+3]##有時它可以是+1##]-1]##

任何人都可以幫我用正則表達式來替換所有這樣的字符串爲空。

謝謝。

+0

這是什麼,你試圖達到什麼目的?你想要分割的字符串是什麼,輸出結果是什麼,你到目前爲止嘗試過了什麼? –

+0

我有客戶評論文件。在該文件中,數據格式不正確。 –

+0

注意:正如現在寫的,你允許有一個加號或一個數字。那真的是你想要的嗎? – GhostCat

回答

0

使用下面的正則表達式的內容相匹配的內部[ ]也跟着文本形式的任何(##|*)

\[(.*?)\]|(##|\*) 

\[(.*?)\]它將匹配的話[t] [+3] [+2] [+2] [+1] [+2] [+2] [+1](##|\*)將匹配的話##* or ##

Java代碼:

String data="[+3]##i recently purchased the canon powershot g3 and am extremely satisfied with the purchase . use"; 
    String regex="\\[(.*?)\\]|(##|\\*)"; 
    System.out.println(data.replaceAll(regex, "")); 

輸出:
i recently purchased the canon powershot g3 and am extremely satisfied with the purchase . use

它會爲您的文件正常工作。

0

您可以使用下面的正則表達式:如果您使用的是Java,那麼

"HERE YOUR STRING".replaceAll("^[+-][0-9]{1,}[\]#]{1,}", ""); 

如果你使用bash/sh的

^[+-][0-9]{1,}[\]#]{1,} 

echo $your_string | sed 's/^[+-][0-9]\{1,\}[\\]#]{1,}//g' 

如果不適合你,讓我知道。但很高興知道你正在嘗試用哪種語言來做到這一點。

相關問題