2013-07-24 19 views
3

我想要像整數一樣使用$ 1值。
這個想法是用originaltext中的所有數字替換等價的數組值並創建一個新的文本。
下面的期望結果應該是「這是DBValue4,這是DBValue2,這是DBValue7」
此外,有沒有辦法保存這些反向引用以備後用?在正則表達式中使用反向引用來動態替換文本

String[] values = {"DBValue0","DBValue1","DBValue2","DBValue3","DBValue4","DBValue5","DBValue6","DBValue7","DBValue8","DBValue9","DBValue10"}; 
String originaltext = "This is 4, This is 2, This is 7"; 
text = originaltext.replaceAll("(\\d)","$1"); 
// want something like 
text = originaltext.replaceAll("(\\d)",values[$1]); 
//or 
text = originaltext.replaceAll("(\\d)",values[Integer.parseInt("$1")]); 
+0

使用'Pattern'和'Matcher' –

+0

你可以看看這篇文章:http://stackoverflow.com/questions/375420/java-equivalent- to-phps-preg-replace-callback –

回答

3

您可以使用PatternMatcher像這樣:

public static void main(String[] args) throws Exception { 
    final String[] values = {"DBValue0", "DBValue1", "DBValue2", "DBValue3", "DBValue4", "DBValue5", "DBValue6", "DBValue7", "DBValue8", "DBValue9", "DBValue10"}; 
    final String originaltext = "This is 4, This is 2, This is 7"; 
    final Pattern pattern = Pattern.compile("(?<=This is)\\d++"); 
    final Matcher matcher = pattern.matcher(originaltext); 
    final StringBuffer sb = new StringBuffer(); 
    while (matcher.find()) { 
     System.out.println(matcher.group()); 
     final int index = Integer.parseInt(matcher.group()); 
     matcher.appendReplacement(sb, values[index]); 
    } 
    matcher.appendTail(sb); 
    System.out.println(sb); 
} 

輸出:

4 
2 
7 
This is DBValue4, This is DBValue2, This is DBValue7 

編輯

繼OP的評論是似乎OP需求代替String s的形式爲{name, index}其中「name」是數組的名稱,「index」是該數組中元素的索引。

這很容易通過Map平的陣列來實現的,以使用Map<String, String[]>,然後使用Pattern捕獲第一nameindex他們的名字。

public static void main(String[] args) throws Exception { 
    final String[] companies = {"Company1", "Company2", "Company3"}; 
    final String[] names = {"Alice", "Bob", "Eve"}; 
    final String originaltext = "This is {company, 0}, This is {name, 1}, This is {name, 2}"; 
    final Map<String, String[]> values = new HashMap<>(); 
    values.put("company", companies); 
    values.put("name", names); 
    final Pattern pattern = Pattern.compile("\\{([^,]++),\\s*+(\\d++)}"); 
    final Matcher matcher = pattern.matcher(originaltext); 
    final StringBuffer sb = new StringBuffer(); 
    while (matcher.find()) { 
     System.out.println(matcher.group(1)); 
     System.out.println(matcher.group(2)); 
     final int index = Integer.parseInt(matcher.group(2)); 
     matcher.appendReplacement(sb, values.get(matcher.group(1))[index]); 
    } 
    matcher.appendTail(sb); 
    System.out.println(sb); 
} 

輸出:

company 
0 
name 
1 
name 
2 
This is Company1, This is Bob, This is Eve 
+0

對不起,但出於簡單的原因,這個例子並不好。你做了什麼,但我想使用更復雜的東西,比如「String originaltext =」{company,3}將此消息分配給來自{new,11}的號碼{number,10}的{name,2}所有者{ ;「和」text = originaltext.replaceAll(「\\ {。+?,(\\ d \\ d?)\\}」,values [Integer.parseInt(「$ 1」)]);「。有什麼建議麼? –

+0

我不明白 - 所以你有幾個數組,你想在它們之間選擇'String'? –

+0

不,我只想用值[an_int]數組屬性替換任何文本中的{something,an_int}。 –

相關問題