2014-07-24 24 views
0

之間的文本我試圖從字符串中刪除BBCode。找到bbCode

這是myString的:

String wording = "Teststring 
[URL=\"http://www.test.ch/status\"]http://www.test.ch/status[/URL] [IMG]http://sit.corproot.net/uploads/659_untitled.png[/IMG] \n\n" 

我已經試過這一個:

wording?.replaceAll("\\[URL=\".*\\](.*?)\\[/URL\\]", "") 

我的目標字符串應該是:

Teststring http://www.test.ch/status \n\n" 

但是當我運行的代碼,它不會取代任何東西

我做錯了什麼?

親切的問候

+0

爲什麼''後措辭? –

+0

我使用Groovy腳本。這是savenavigation操作員。 – CollinG

+1

我不認爲'/'需要在RegEx中轉義,因此關閉的BBTag從不匹配。通過查看輸入'[URL =「asd」] [\/URL]'來驗證。 – AlexR

回答

0
public static void main(String[] args) { 

    String s = "Teststring URL=\"http://www.test.ch/status\"]http://www.test.ch/status[/URL] [IMG]http://sit.corproot.net/uploads/659_untitled.png[/IMG] \n\n"; 

    Pattern p = Pattern.compile("(?s)^(\\w+)[^\\[]+\\[URL=\"(.*)\"\\].*"); 
    Matcher m = p.matcher(s); 

    if (m.matches()) { 
     System.out.println(String.format("%s %s \\n\\n", m.group(1), m.group(2))); 
    } 

} 

日期:

Teststring http://www.test.ch/status \n\n 
0

正則表達式:

\s*\[URL=\\".*\](.*?)\[\/URL\]|\s*\[IMG\].*?\[\/IMG\]\s* 

Java的正則表達式的字符串將是,

"\\s*\\[URL=\\\\\".*\\](.*?)\\[\\/URL\\]|\\s*\\[IMG\\].*?\\[\\/IMG\\]\\s*" 

替換字符串:

$1 

DEMO