2013-07-19 44 views
0

我在Java中,我需要做一個替換,並需要一些正則表達式的幫助。動態文本的正則表達式

String temp = "/Users/john/core-xxx/testSomething.java" 

我使用xxx作爲動態文本的佔位符。所以我的目標是用名爲testingPlatform的文本替換core-xxx。感謝您的幫助

因此System.println(someNewVariable);應顯示

"/Users/john/testingPlatform/testSomething.java" 
+0

它不會是XXX。它可以是core-doc或core-sos – jrock2004

回答

4

你可以做

String newTemp = temp.replaceAll("core-\\w+", "testingPlatform"); 

其中\w+匹配一個或多個單詞字符([a-zA-Z_0-9]

文件名可以包含Unicode字符,以便更好地

String newTemp = temp.replaceAll("core-\\p{L}+", "testingPlatform"); 
0

使用此replaceAll ...

replaceAll("yours","Replacement"); 
+0

在輸入字符串「core-xxx」中,xxx部分是動態的,所以它可以是core-abc,core-efg等等。所以OP想要一個正則表達式 – sanbhat

0

你應該看看的String

replacereplaceAll方法在你的情況,你應該做到以下幾點:

System.println(temp.replaceAll("core-\\w+", "testingPlatform")); 
+1

不知道這個是一個答案..應該是一個評論? – sanbhat

+0

我剛剛添加了一個關於如何獲得用戶正在尋找的示例。 – mariosangiorgio