Regular expression number java

Java regex validate 10 digit number

This regular expression refers to a pattern which accepts 10 digits number only.

Example

package com.w3spoint; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexTest { private static final String PATTERN = "\d"; public static void main(String args[]){ ListString> values = new ArrayListString>(); values.add("Jai"); values.add("12345345"); values.add("1234567890"); Pattern pattern = Pattern.compile(PATTERN); for (String value : values){ Matcher matcher = pattern.matcher(value); System.out.println(matcher.matches()); } } }

package com.w3spoint; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexTest < private static final String PATTERN = "\d"; public static void main(String args[]) < Listvalues = new ArrayList(); values.add("Jai"); values.add("12345345"); values.add("1234567890"); Pattern pattern = Pattern.compile(PATTERN); for (String value : values) < Matcher matcher = pattern.matcher(value); System.out.println(matcher.matches()); >> >

Output

Источник

Numbers only regex (digits only) Java

Numbers only (or digits only) regular expressions can be used to validate if a string contains only numbers.

Basic numbers only regex

Below is a simple regular expression that allows validating if a given string contains only numbers:

Enter a text in the input above to see the result

Real number regex

Real number regex can be used to validate or exact real numbers from a string.

Enter a text in the input above to see the result

 > 

Enter a text in the input above to see the result

Notes on number only regex validation

In Java you can also validate number by trying to parse it:

 catch (NumberFormatException nfe)

Create an internal tool with UI Bakery

Discover UI Bakery – an intuitive visual internal tools builder.

Источник

How to check if a String is Number in Java — Regular Expression Example

In order to build a regular expression to check if String is a number or not o r if String contains any non-digit character or not you need to learn about character set in Java regular expression, Which we are going to see in this Java regular expression example. No doubt Regular Expression is a great tool in a developer’s arsenal and familiarity or some expertise with regular expression can help you a lot. Java supports regular expression using j ava.util.regex.Pattern and java.util.regex.Matchter class, you can see a dedic ated package java.util.regex for a regular expression in Java. Java supports regex from JDK 1.4, which means well before Generics, Enum, or Autoboxing.

If you are writing server-side code in Java programming language then you may be familiar with the importance of regular expression which is key in parsing certain kinds of messages e. g. FIX Messages used in Electronic trading.

In order to par se Repeating groups in FIX protocol y ou really need an understanding of regular expression in Java. Any way In order to learn validating numbers using regular expression we will start with a simple example

1) Check if a String is a number or not using regular expression

to clarify the requirement, a String would be a number if it contains digits. we have omitt ed decimal point a nd sign or + or — for simplicity.

If you are familiar with a predefined character class in Java regular an expression that you must know that \d will represent a digit (0-9) and \D will represent a non-digit (anything other than 0 to 9). Now using this predefined character class, a String will not be a number if it contains any non digit characters, which can be written in Java regular expression as:

which checks for non digit character anywhere in th e String. T his pattern return true if String contains any thing other than 0-9 digit, which can be used to know if an String is number or not using regular expression.

Same regular expression for checking String for numbers can also be written without using predefined character set and using character class and negation as shown in following example :

This is similar to the above regex pattern, the only difference is \D is replaced by [^0-9]. By the way, there are always multiple ways to check for certain things using regex.

2. Verify if a String is a six-digit number or not using regular expression

This is a kind of special regular expression requirement for validating data like id , zipcode or any other pure numerical data. In order to check for digits you can either use character class 7 or use short-form \d . here is a simple regular expression in Java which can check if a String contains 6 digits or not:

Pattern digitPattern = Pattern. compile ( » \\ d \\ d \\ d \\ d \\ d \\ d» ) ;

above pattern checks each character for digit six times. This pattern can also be written in much shorter and readable format as :

where <6>denote six times. you can also replace \d with character class 7 and it should work. You can further see these Java Regular expression courses to learn more about different regular expression patterns.

Code Example — Regular expression in Java to check numbers

Here is a complete code example in Java programming language to check if a String is an integer number or not. In this Java program, we are using regular expressions to check if String contains only digits i.e. 0 to 9 or not. If String only contains a digit then its number otherwise it’s not a numeric String.

One interesting point to note is that this regular expression only checks for integer numbers as it not looking for dot(.) characters, which means floating point or decimal numbers will fail this test.

import java.util.regex.Pattern ;
/**
* Java program to demonstrate use of Regular Expression to check

* if a String is a 6 digit number or not.
*/
public class RegularExpressionExample

public static void main ( String args [])

// Regular expression in Java to check if String is a number or not
Pattern pattern = Pattern. compile ( «.*[^0-9].*» ) ;
//Pattern pattern = Pattern.compile(«.*\\D.*»);
String [] inputs = < "123" , "-123" , "123.12" , "abcd123" >;

for ( String input: inputs ) <
System. out . println ( «does » + input + » is number : «

+ ! pattern. matcher ( input ) . matches ()) ;
>

// Regular expression in java to check if String is 6 digit number or not
String [] numbers = < "123" , "1234" , "123.12" , "abcd123" , "123456" >;
Pattern digitPattern = Pattern. compile ( » \\ d» ) ;

+ digitPattern. matcher ( number ) . matches ()) ;
>
>

Output:
does 123 is number : true
does — 123 is number : false
does 123.12 is number : false
does abcd123 is number : false

does 123 is 6 digit number : false
does 1234 is 6 digit number : false
does 123.12 is 6 digit number : false
does abcd123 is 6 digit number : false
does 123456 is 6 digit number : true

That’s all on using Java regular expression to check numbers in String. As you have seen in this Java Regular Expression example that it’s pretty easy and fun to do the validation using regular expression.

Источник

Читайте также:  Find php version linux
Оцените статью