2

Is there any difference practically between [a-zA-Z] and [a-z[A-Z]]?

According to java tutorial (and Pattern class javadoc):

[a-zA-Z]    a through z, or A through Z, inclusive (range)
[a-d[m-p]]  a through d, or m through p: [a-dm-p] (union)

Java tutorial: character classes

Ilya Bystrov
  • 2,902
  • 2
  • 14
  • 21
  • 2
    Range is `a-z`, union of ranges is `[a-zA-Z]` which in Java can also be written as `[a-z[A-Z]]` or `[[a-z][A-Z]]`. – Pshemo Nov 12 '14 at 22:08
  • 3
    I suggest reading a better [article](http://www.rexegg.com/regex-class-operations.html#union), it is pretty thorough. – hwnd Nov 12 '14 at 22:11

1 Answers1

2

Not much as far as I can see, other than making certain regexes easier to read. @hwnd's link to the regexx article gives good examples.

I've come up with a fairly esoteric use-case that might warrant it though: a character-class builder. You could build a character class by wrapping any number of other character classes in [] and you wouldn't have to worry about stripping [] from the various constituent classes.

Paul Hicks
  • 13,289
  • 5
  • 51
  • 78