Regex for phone number with standard phone number using brackets

This is a RegEx question in Java. I searched everywhere, but could not find anything similar to what I needed. In addition, I spent several hours trying on my own.

I am in the last part of the school lab encoded in Java. I use Eclipse as my compiler. I created a membership application using Swing components,

One of my fields is for entering a 10-digit phone number. However, I had to make this JFormatted text field masking the text field in this format: "(###) ### - ####". Therefore, when the program loads, this text box looks like this: "( _) -___"

Now, all I have to do is have a regular expression that will allow the user to enter 10 digits. However, I need it to work with the disguised format that I used above. This is what I still have:

pattern = Pattern.compile("\\d{3}\\s\\d{3}-\\d{4}");
    matcher = pattern.matcher(phoneText.getText());
    if (!matcher.find())
    {
        phoneText.requestFocus();
        JOptionPane.showMessageDialog(null, "Phone entered incorrectly.  Phone should be:\n" +
                "10 numeric digits.",
                "A D D   E R R O R",JOptionPane.ERROR_MESSAGE);
        return;
    }

I believe that I have regEx written where it takes space and dashes in mask format, but I don't know how to include brackets.

Edit: adding the question in the correct format to the advice of Andrew Thomas:

What do I need to add to my regEx to include brackets that are already in a formatted text box?

Edit2: The answer to the question, but adding samples to the Bohemian Council so that future students who encounter this message can use it. Sorry, I'm new to this!

You need to look like this:

(123) 456-7890

+5
source share
3

( ) ( 3 )

:

pattern = Pattern.compile("\\(\\d{3}\\)\\s\\d{3}-\\d{4}");
                           ^^^      ^^^

, \\ ( ).

. Java, \\ ( \ Java).

, : :

" ( , (".

+4

\\( \\) , :

Pattern.compile("\\(\\d{3}\\)\\s\\d{3}-\\d{4}");
+2

Here is a quick solution when you need to add brackets to the text box / phone number field, note that this is not a jquery based regex

http://ashfaqahmed.net/jquery-add-prefix-postfix-on-phone-number-field/

0
source

All Articles