Regex is a powerful way to work with strings. It has many components such as anchors, qualifiers, or operators, character classes, flags and more.
The following regex is an example of a string separator using regex. With this regex patter you can separate any type of phone number layout into first, second and third sections.
.?(\\d{3}).*(\\d{3}).*(\\d{4})
.?
- 0 or 1 character to account for the optional open parenthesis
.*
- 0 or more characters to account for the optional closing parenthesis, hyphen, and space characters
.*
0 or more characters to account for the optional hyphen and space characters
(\\d{3})
- 3 digit characters (first capture group i.e. first 3 digits)
(\\d{3})
3 digit characters (second capture group i.e. next 3 digits)
(\\d{4})
4 digit characters (third capture group i.e. last 4 digits)
(\\d{3})
- 3 digit characters (first capture group i.e. first 3 digits)
(\\d{3})
3 digit characters (second capture group i.e. next 3 digits)
(\\d{4})
4 digit characters (third capture group i.e. last 4 digits)
.?
- 0 or 1 character to account for the optional open parenthesis
.*
- 0 or more characters to account for the optional closing parenthesis, hyphen, and space characters
By Jacob Adelman. Github jakeadelman