Why should you use composite character classes?

We should use composite character classes for the following reasons:

  • To create new custom character classes from predefined Unicode blocks. For example, to match all the letters in an Arabic Unicode block, we can use the following:
[\p{InArabic}&&\p{L}] 
  • To avoid potentially slower lookahead or lookbehind patterns by using the intersection or subtraction operation on multiple character classes.
  • To enhance the readability of the regular expression.

Get Java 9 Regular Expressions now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.