Java regular expression to separate phone number and text in brackets

I would like a regex for Java that can take this line

+1 7183541169 (East Coast)

And create two groups

  • +1 7183541169
  • East Coast.

I'm having difficulty getting out of parentheses.

+3
source share
2 answers

Must be:

^(.*)\((.*)\)$

In this case, there is no special format - it will accept numbers or letters anywhere. The regular expression reads:

^- Start of line
(.*)- some letters (captured group)
\(- literal (
(.*)- more letters (captured group)
\)- literal )
$- end of line

, , .

+5
/^(\+\d{1} \d+) \(((?:\w| |-)+)\)$/i

, .

0

Source: https://habr.com/ru/post/1752046/


All Articles