我有一个字符串:
Expected attribute value 'internalClosureRule' but was 'internalClosureRule2' - comparing <Option name="internalClosureRule"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name to <Option name="internalClosureRule2"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name,
Expected attribute value 'opt_cpd_p2s_skip' but was 'opt_cpd_p2s_skip2' - comparing <Option name="internalClosureRule"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name to <Option name="internalClosureRule2"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name
我想从第一个“ to”中删除“ Value”,直到第一个逗号(,),然后对字符串中“,”分隔的值使用相同的模式,即 我想将我的字符串打印在单独的行中,该行没有“ to”,直到其“,”逗号都在末尾。
我的预期输出是:
Expected attribute value 'internalClosureRule' but was 'internalClosureRule2' - comparing <Option name="internalClosureRule"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name,
Expected attribute value 'opt_cpd_p2s_skip' but was 'opt_cpd_p2s_skip2' - comparing <Option name="internalClosureRule"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name,
有人可以提供帮助吗?
答案 0 :(得分:1)
实现该目标的一种方法是使用带有正则表达式的String#replaceAll
方法并将其替换为空字符串(""
)。就您而言,这种模式很容易,而且很容易
to.*,
其中:
to
->表示“至”字。
.*
->任意字符
,
->逗号
@Test
public void fromFirstToTillComma()
{
String string = "Expected attribute value 'internalClosureRule' but was 'internalClosureRule2' - comparing <Option name=\"internalClosureRule\"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name to <Option name=\"internalClosureRule2\"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name,";
String expected = "Expected attribute value 'internalClosureRule' but was 'internalClosureRule2' - comparing <Option name=\"internalClosureRule\"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name,";
string = string.replaceAll("to.*,", "").trim();
string += ",";
assertEquals(expected, string);
}
有关Pattern
的更多信息,请参见here.
评论后: 与其同时处理所有值,不如通过用逗号分割值,将问题分割成较小的值。然后,您将每个值分开,并且可以使用我的正则表达式。最后,再次将值加起来并用逗号分隔。
代码:
@Test
public void fromFirstToTillComma() {
String string = "Expected attribute value 'internalClosureRule' but was 'internalClosureRule2' - comparing <Option name=\"internalClosureRule\"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name to <Option name=\"internalClosureRule2\"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name,"
+ "Expected attribute value 'opt_cpd_p2s_skip' but was 'opt_cpd_p2s_skip2' - comparing <Option name=\"internalClosureRule\"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name to <Option name=\"internalClosureRule2\"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name,";
String expected = "Expected attribute value 'internalClosureRule' but was 'internalClosureRule2' - comparing <Option name=\"internalClosureRule\"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name,"
+ "Expected attribute value 'opt_cpd_p2s_skip' but was 'opt_cpd_p2s_skip2' - comparing <Option name=\"internalClosureRule\"...> at /TCXML[1]/Header[1]/TransferFormula[1]/OptionSet[1]/Option[1]/@name,";
String lines[] = string.split(",");
StringBuilder sb = new StringBuilder();
for (String line : lines) {
line += ","; //Add the comma at the end because string.split(",") removed it
line = line.replaceAll("to.*,", "").trim();
sb.append(line);
sb.append(","); //Each value separated with comma
}
String clear = sb.toString().trim();
assertEquals(expected, clear);
}