0

I saw one code example and didn't understand how this prints only Print statement. Appreciate your help on this.

String str = "<a href=/utility/ReportResult.jsp?reportId=5>Print</a>";
System.out.println(str.replaceAll("\\<.*?\\>", ""));

OutPut: Print

How to modify my regex expression to print Print<>Report instead of PrintReport. Below is my regex and statement. String str = "Print<>Report";

    System.out.println(str.replaceAll("<.*?>", ""));
Veera
  • 309
  • 2
  • 3
  • 12

4 Answers4

1

You don't have to escape the < (angular braces). So in java str.replaceAll("<.*?>", "") will be sufficient.

How it works :

<.*?> --> Search for first < then match everything until the next >. Note that .*? is called lazy selector / matcher.

TheLostMind
  • 34,842
  • 11
  • 64
  • 97
  • Thanks for your answers. How can I modify my regex to print Print<>Report instead of PrintReport. Below is my regex and statement. String str = "Print<>Report"; System.out.println(str.replaceAll("<.>", "")); – Veera Jan 28 '15 at 11:53
1

In order to print Print<>Report instead of PrintReport, change the * by +:

System.out.println(str.replaceAll("<.+?>", ""));
//                            here __^

* means 0 or more precedent character
+ means 1 or more precedent character

Toto
  • 83,193
  • 59
  • 77
  • 109
0

Its a Regex says anything b/w "<" and ">" must be repalce by ""(blank string) So

<a href=/utility/ReportResult.jsp?reportId=5>==> ""(blank)
</a>==>""(blank)

and only "Print" left

squiroid
  • 13,119
  • 4
  • 43
  • 67
0

First, the leading backslashes are treated as an escape sequence for Java, so the actual regular expression is \<.*?\>

The \<' matches the<` character (the backslash again is an escape sequence, which indicates that the following character should be interpreted literally and not as a regex operator). This is the beginning of an html tag.

The . token matches any character.

The ? is a reluctant quantifier that indicates that the preceding token (any character in this case) should be matched zero or more times.

The /> matches the end of a tag. Because the ? is reluctant, the . does not match the character(s) that can be matched by this token.

biggvsdiccvs
  • 289
  • 2
  • 12