-1

I have a long text like below:

name="sessionValidity" value="2018-09-13T16:28:28Z" type="hidden" name="shipBeforeDate" value= "2018-09-17" name="merchantReturnData" value= "",name="shopperLocale" value="en_GB" name="skinCode" value="CeprdxkMuQ" name="merchantSig" value="X70xAkOaaAeWGxNgWnTJolmy6/FFoFaBD47IzyBYWf4="

Now, I have to find all the data which are stored in the value string. Please help.

Jack Flamp
  • 1,223
  • 1
  • 16
  • 32
Ranjan Gupta
  • 255
  • 2
  • 12

1 Answers1

0

Usually the worst thing you could do is parsing an HTML with regex. Detailed explonation here.

For the purpose of parsing the data and manipulate it the right way you should considering using an advanced markup parser like jaxb, jsoup, or any other.

Of course it is a case specific decision and in your case maybe this one could do the work...

private static List<String> extractValuesAsUselessList(String theString) {
    List<String> attributes = new ArrayList<>();

    if (theString != null && !theString.equals("")) {
        Matcher matcher = Pattern.compile("\\s([\\w+|-|:]+)=\"(.*?)\"").matcher(theString);

        while (matcher.find()) {
            if ("value".equals(matcher.group(1))) {
                attributes.add(matcher.group(2));
            }
        }
    }

    return attributes;
}
dbl
  • 1,109
  • 9
  • 17
  • Thanks for the suggestion. I am trying to modify the data through jsoup now. I have the below html: – Ranjan Gupta Sep 25 '18 at 09:01
  • I think you did the right choice. Good luck with it and don't bother asking for any assistance in this direction. – dbl Sep 25 '18 at 09:05
  • I tried modifying specific value through jsoup like below: Elements parents = doc.select("input[value]"); for (Element parent : parents) { System.out.println(parent.attr("value").replace("X70xAkOaaAeWGxNgWnTJolmy6/FFoFaBD47IzyBYWf4=", "Ranjan").replace("17572418", "17572418123").replace("200", "199").replace("2018-09-13T16:28:28Z", "2018-09-25T16:28:28Z").replace("2018-09-17", "2018-09-25")); But When I print System.out.println(doc); It is printing the same old value instead I should get the modified one. How to modify the specific value which are under input tag? – Ranjan Gupta Sep 25 '18 at 09:08
  • please help if you have any insight to the above issue which I am facing currently. – Ranjan Gupta Sep 25 '18 at 09:16