-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,082
  • 2
  • 26
  • 46

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
  • 166,785
  • 24
  • 204
  • 249
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,601
  • 13
  • 59
  • 69