2015-12-09 95 views
2

与新的格局正则表达式我得到了我没有能够解决的问题的Java替换字符串与阵列

我有一个包含一些关键字需要与存储在一个新的值来代替一个段落阵列

实施例:

段落: “我的最喜欢的水果是[0],但我也喜欢[1]和[3]”

阵:水果= “香蕉”, “橙色”, “苹果”, “葡萄”]

我的期望是:My most favorite fruit is Banana, but I also like Orange and Grape

你能帮我找到一个解决方案?

我已经试过我的句子转换成字符串数组是这样的:

["My most favorite fruit is ","[0]",", but I also like ","[1]"," and ","[3]"]

在那之后,我更换[0]0,我得到这个:

["My most favorite fruit is ","0",", but I also like ","1"," and ","3"]

我倾向于将上述阵列中的0,13替换为值fruits[0]fruits[1]fruits[3]然后再转换阵列成一个完整的串

但我认为这不是最好的解决办法,因为如果我得到了一个输入句子是这样的:"2[2]"那么我会收到输出AppleApple,而展望是2Apple

+3

如果您的问题“无法解决”你为什么要张贴? – Moob

回答

1

如果你能够改变paragraph的格式,那么你可以从这段代码开始。

String paragraph = "My most favorite fruit is %s, but I also like %s and %s"; 
String[] fruits = {"Banana", "Orange", "Apple", "Grape"}; 
System.out.printf(paragraph, fruits[0], fruits[1], fruits[2]);     

输出

My most favorite fruit is Banana, but I also like Orange and Apple 

编辑另一种解决方案,不必保持水果的位置参数可能。

String paragraph = "My most favorite fruit is {0}, but I also like {1} and {3}"; 
Object[] fruits = {"Banana", "Orange", "Apple", "Grape"}; 
MessageFormat mf = new MessageFormat(paragraph); 
System.out.println(mf.format(fruits)); 

输出

My most favorite fruit is Banana, but I also like Orange and Grape 
+0

我想用source段落中的精确索引替换,就像[0]应该用水果[0]替换,等等...... – mrzenky

+0

@mrzenky这是否意味着占位符可以是随机顺序的。例如。 '李四喜欢[2]和[1],但不喜欢[3]'应导致为'李四喜欢苹果和橘子,但不喜欢Grape'? – SubOptimal

+0

@mrzenky您可以使用该'%1 $ s'。参见[格式化语法(https://docs.oracle.com/javase/8/docs/api/java/util/Formatter.html#syntax)。 –

0

您可以使用下面的代码(见demo):

ArrayList<String> fruits_arr = new ArrayList<String>(); // Just initializing the array 
     fruits_arr.add("Banana");   
     fruits_arr.add("Orange"); 
     fruits_arr.add("Apple"); 
     fruits_arr.add("Grape"); 
    String[] fruits = fruits_arr.toArray(new String[0]); 
    String s = "My most favorite fruit is [0], but I also like [1] and [3]"; 
    StringBuffer result = new StringBuffer(); 
    Matcher m = Pattern.compile("\\[(\\d+)\\]").matcher(s); // Initializing Matcher 
    while (m.find()) {       // Iterate over matches 
     int num = Integer.parseInt(m.group(1)); // If there is a match, Group 1 has digits 
     String replacement = ""; 
     if (num < fruits.length) {   // If the number is lower than fruit element count 
       replacement =fruits[num]; // Get the array element 
     } else { 
      replacement = m.group();  // Else, use the whole match (e.g. [9]) 
     } 
     m.appendReplacement(result, replacement); // Append this replacement 
    } 
    m.appendTail(result); 
    System.out.println(result.toString()); 

随着\[(\d+)]你搭配[ + digits + ]任何字符串,并捕获数字序列到组1.

+0

像你这样使用PHP?我可以用于Java吗? – mrzenky

+0

我已将代码重新写入Java。是的,它类似于PHP,JS和其他语言,我们在Replace函数的回调函数中执行字符串操作。 –

2

使用字符串。更换

String sentence = "My most favorite fruit is [0], but I also like [1] and [3]"; 
String[] replacements = {"Banana", "Orange", "Apple", "Grape"}; 
for(int i = 0; i < replacements.length; i++) 
    sentence = sentence.replace("[" + i + "]", replacements[i]); 
+0

非常感谢,它的工作原理 – mrzenky

0

您可以使用以下方法:

String str = "My most favorite fruit is [0], but I also like [1] and [3]"; 
    String[] fruits = { "Banana", "Orange", "Apple", "Grape" }; 

    for (int i = 0; i < fruits.length; i++) 
    { 
     str = str.replaceAll("\\[" + i + "\\]", fruits[i]); 
    } 
3

Java的string formatting有一个内置的语法这一点。一般格式为:

%[argument_index$][flags][width][.precision]conversion 

因此您可以使用例如%1$s意味着第一个格式参数,%2s第二等的注意,索引都是一个,而不是从零开始。

例如

String[] fruits = {"Banana", "Orange", "Apple", "Grape"}; 
System.out.format(
    "My most favorite fruit is %1$s, but I also like %2$s and %4$s", 
    fruits);