1

The following code is used to remove html tags from the string

s = s.replaceAll("\\<[^>]*>","");

The following code also gives the same result:

s = s.replaceAll("<[^>]*>","");

What is the difference? Why should we add double slash at the front?

What is the use of . in regex? Can you explain with an example?

bitsapien
  • 1,574
  • 11
  • 23
Nikeshh Baskaran
  • 193
  • 4
  • 15
  • Have you tried reading documentation? There is nice javadoc out there, explaining the meaning of all elements of characters in regular expressions. The dot is a wildcard matching any character btw. – GhostCat Jun 24 '18 at 04:57
  • *Why should we add double slash at the front?* - You shouldn't. – Wiktor Stribiżew Jun 24 '18 at 09:01

1 Answers1

3

It is not needed. As you pointed out, both do the same thing. Here is why...

In Java Regular expressions, \\ is a single backslash. Backslashes are used to escape the next character. The next character is a < which does not need to be escaped, therefore the \\< is redundant, and can be replaced with just <.

Look here for characters that have special meaning and/or need to be escaped: http://docs.oracle.com/javase/6/docs/api/java/util/regex/Pattern.html

Say you were trying to match a ? instead of the <, then you would use a regex like \\?.

To match a single backslash, you would need 4 backslashes \\\\ in your regex.

Also note, If you were to type this line into an IDE like IntelliJ IDEA, it will highlight it and say:

Redundant character escape '\\<' in RegExp

D. May
  • 293
  • 1
  • 7