gpt4 book ai didi

java - 如何在java中删除字符串的第一个 "to"到第一个逗号(,)之间的值

转载 作者:行者123 更新时间:2023-11-30 05:38:13 24 4
gpt4 key购买 nike

我有一个字符串:

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”中的值,直到第一个逗号(,),并且对于字符串中的“,”分隔值应采用相同的模式,即我想在单独的行中打印我的字符串,其中没有“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,

有人可以提供帮助吗?

最佳答案

实现此目的的一种方法是使用带有正则表达式的 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);
}

有关模式的更多信息,请参见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);
}

关于java - 如何在java中删除字符串的第一个 "to"到第一个逗号(,)之间的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56219315/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com