How to remove the value from the first "to" and till the first comma (,) for a string in java

I have a 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

I would like to remove the Value from the first "to" and till the first comma (,) and like that the same pattern should go on for the "," separated values in the string i.e. i want to print my string in separate line which are without the "to" and till its "," comma which is at the end.

My Expected Output is:

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,

Could someone please provide some help?

Upvotes: 1

Views: 79

Answers (1)

George Z.
George Z.

Reputation: 6808

One way to achieve that is by using String#replaceAll method with a regular expression and replace it with empty string (""). In your case the pattern is easy and it is

to.*,

where:

to -> for "to" word.

.* -> for any character

, -> for comma

    @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);
    }

More about Patterns can be found here.


After comment: Instead of treat all the values at the same time, split the problem into smaller ones, by splitting the values by comma. Then you will have each value separated and you can use my regular expression. Finally sum the values again separated with comma.

Code:

@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);
}

Upvotes: 1

Related Questions