-1

My goal is to validate regex for wildcard in domain addresses;

I've tried to do it through java.util.regex.Pattern, but some cases confuse me. Can someone explain, why it considered as valid pattern:

Pattern.compile("h]cat")

And this one as not valid:

Pattern.compile("h[cat")
fashuser
  • 2,152
  • 3
  • 29
  • 51

2 Answers2

2
  • h]cat in this string ] means a literal ] symbol.

  • h[cat in this string, a character class is started [ but without termination. So it's not valid. [ considered as start of char class.

Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
2

It's a syntax thing. { and } can exist on themselves as literal, and so does ] when it's alone. An unclosed [ however, is syntax error.

Unihedron
  • 10,902
  • 13
  • 62
  • 72