Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- You are given an HTML document given as a string on multiple lines.
- Write a program that replaces all the tags <a href=…>…</a> with corresponding tags [URL href=…]…[/URL].
- Read lines until you get the "END" command.
- Note: a tag can start and end on different lines, but actual keywords like "href=" or the closing tag "</a>" will never be split.
- For example, you will never get:
- <a hr
- ef="http://softuni.bg">SoftUni</
- a>
- Hints
- • Use a string builder to read the whole input into a single string. Make sure to save the new lines.
- • Create a pattern that can match a valid tag on multiple lines. Note that href= and </a> will never be split.
- • Use a string builder to replace all matches.
- Examples:
- <ul> <li> <a <ul> <li> [URL href="http://softuni.bg"]SoftUni[/URL]
- href="http://softuni.bg">SoftUni</a> -> </li> </ul>
- </li> </ul>
- END
- <a href="/"> [URL href="/"]
- Link</a> -> Link[/URL]
- END
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class ReplaceA_Tag {
- public static void main(String[] args) {
- StringBuilder htmlText = new StringBuilder();
- try (BufferedReader reader =
- new BufferedReader(new InputStreamReader(System.in))) {
- String inputLine = reader.readLine();
- while (!inputLine.equals("END")) {
- htmlText.append(inputLine).append(System.lineSeparator());
- inputLine = reader.readLine();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- String output = htmlText.toString()
- .replaceAll("<a\\s([^>]+)>([^<]+)</a>", "[URL $1]$2[/URL]");
- System.out.println(output);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement