We will learn more about our regular expression String in next section, when we will see our code example for check if String contains a number or not.
Following code sample is our complete Java program to check if String contains any number or not.
This regular expression and String example, also teaches best practices about regex.
means a word character (any lowercase letter, any uppercase letter, the underscore character, or any digit). They make your code easier to read and eliminate errors introduced by malformed character classes.
Copy the code below to clipboard; Select src folder in your project; Press CTRL V; Eclipse IDE will automatically create package & class file with the code that’s pasted from clipboard.
You can use this regular expression to separate alphabetic string from alphanumeric ones.
Enter your regex: \d Enter input string to search: a No match found.
Enter your regex: \D Enter input string to search: 1 No match found.
In this regex tutorial, we will learn to validate user entered phone numbers for a specific format (in this example numbers are formatted in north American format) and if numbers are correct then reformat them to a standard format for display. ([0-9])$"; Pattern pattern = Pattern.compile(regex); for(String email : phone Numbers) Output: 1234567890 : true 123-456-7890 : true 123.456.7890 : true 1 : true (123) 456 7890 : true 12345678 : false 12-12-111 : false List phone Numbers = new Array List(); phone Numbers.add("1234567890"); phone Numbers.add("123-456-7890"); phone Numbers.add("123.456.7890"); phone Numbers.add("1"); phone Numbers.add("(123) 456 7890"); //Invalid phone numbers phone Numbers.add("12345678"); phone Numbers.add("12-12-111"); String regex = "^\(?
I have tested formats including 1234567890, 123-456-7890, 123.456.7890, 1, (123) 456 7890, and all such combinations.
Fortunately, there is a simple, industry-standard notation specified by ITU-T E.123. The significance of this is that EPP-style international phone numbers are increasingly used and recognized, and therefore provide a good alternative format for storing (and validating) international phone numbers. NNNNNNNNNNx EEEE, where C is the 1–3 digit country code, N is up to 14 digits, and E is the (optional) extension.