0

I have a String in Java :

style="hello World">One-time meetings< style=\"Hello Again"> stop "Hello" Sample Input I want to remove all the strings that are between " ", occurring immediately after each occurrence of the String "Style".

So, after the removal, the above String will look like :

style="">One-time meetings< style=\""> stop "Hello"

~Thanks

Navchetan
  • 153
  • 1
  • 2
  • 11

2 Answers2

2

If you want to remove all strings that are between the quotes in the style attribute then a simple replaceAll() should do the trick:

String input = "style=\"hello World\">One-time meetings< style=\"Hello Again\"> stop \"Hello\"";
input = input.replaceAll("style=\"(.*?)\"", "style=\"\"");

Update:

From inspecting your raw input, it appears that the quotes inside the <style> tags themselves are already escaped by a single backslash. If this be the case, then the following replacement should give you what you want:

String input = "style=\\\"hello World\\\">One-time meetings< style=\\\"Hello Again\\\"> stop \"Hello\"";
input = input.replaceAll("style=\\\\\"(.*?)\\\\\"", "style=\\\\\"\\\\\"?");
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • I have a big HTML file, and this code is not working for that.

    Recurring meetings
    These meetings also have a unique meeting ID . To create this type of session, you can select "Occurs multiple times" while scheduling your meeting.

  • Instant meetings

  • – Navchetan Jun 13 '16 at 04:39
  • Saying `not working` doesn't let me help you. What is happening which you should not expect? Keep in mind that if you have defects in your data (e.g. broken tags, typos), then my solution may not work. – Tim Biegeleisen Jun 13 '16 at 04:40
  • I understand. Well its a bigger JSON file with HTML tag etc in it. Which i have no control over. But, the style=\"SomeString " multiple pattern is always there. – Navchetan Jun 13 '16 at 04:42
  • What exactly is going wrong? Does the replacement work anywhere? – Tim Biegeleisen Jun 13 '16 at 04:57
  • I uploaded snippet of the input JSON text that I am parsing. I highlighted the occurrences of "style". Please take a look. I tried your code but it is not working with any occurrence of "style" – Navchetan Jun 13 '16 at 05:08
  • Wait...does your raw data actually escape the quotes in `style`? – Tim Biegeleisen Jun 13 '16 at 05:12
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/114494/discussion-between-tim-biegeleisen-and-navchetan). – Tim Biegeleisen Jun 13 '16 at 05:20
  • Yes, in my RAW data I do this before running your code : replaceAll("\\\\n", ""); – Navchetan Jun 13 '16 at 05:20