Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the Java regular expression for an only integer numbers string?

Tags:

java

regex

I'm trying with if (nuevo_precio.getText().matches("/^\\d+$/")) but got not good results so far...

like image 681
diegoaguilar Avatar asked May 02 '13 06:05

diegoaguilar


3 Answers

In Java regex, you don't use delimiters /:

nuevo_precio.getText().matches("^\\d+$")

Since String.matches() (or Matcher.matcher()) force the whole string to match against the pattern to return true, the ^ and $ are actually redundant and can be removed without affecting the result. This is a bit different compared to JavaScript, PHP (PCRE) or Perl, where "match" means finding a substring in the target string that matches the pattern.

nuevo_precio.getText().matches("\\d+") // Equivalent solution

It doesn't hurt to leave it there, though, since it signifies the intention and makes the regex more portable.


To limit to exactly 4 digit numbers:

"\\d{4}"
like image 196
nhahtdh Avatar answered Oct 22 '22 22:10

nhahtdh


As others have already said, java doesn't use delimiters. The string you're trying to match doesn't need the trailing slashes, so instead of /^\\d+$/ your string should've been ^\\d+$.

Now I know this is an old question, but most of the people here forgot something very important. The correct regex for integers:

^-?\d+$

Breaking it down:

^         String start metacharacter (Not required if using matches() - read below)
 -?       Matches the minus character (Optional)
   \d+   Matches 1 or more digit characters
       $  String end metacharacter (Not required if using matches() - read below)

Of course that in Java you'd need a double backslash instead of the regular backslash, so the Java string that matches the above regex is ^-?\\d+$


NOTE: The ^$ (string start/end) characters aren't needed if you're using .matches():

Welcome to Java's misnamed .matches() method... It tries and matches ALL the input. Unfortunately, other languages have followed suit :(

- Taken from this answer

The regex would still work with the ^$ anyways. Even though it's optional I'd still include it for regex readability, as in every other case when you don't match the entire string by default (which is most of the time if you're not using .matches()) you'd use those characters


To match the opposite:

^\D+$

The \D is everything that is not a digit. The \D (Non digits) negates \d (digits).

Integer regex on regex101


Notice that this is just for integers. The regex for doubles:

^-?\d+(\.\d+)?$

Breaking it down:

^         String start metacharacter (Not required if using matches())
 -?               Matches the minus character. The ? sign makes the minus character optional.
   \d+           Matches 1 or more digit characters
       (          Start capturing group
        \.\d+     A literal dot followed by one or more digits
             )?   End capturing group. The ? sign makes the whole group optional.
               $  String end metacharacter (Not required if using matches())

Of course that in Java instead of \d and \. you'd have double backslashes as the above example does.

Doubles regex on regex101

like image 34
NonameSL Avatar answered Oct 22 '22 20:10

NonameSL


Java doesn't use slashes to delimit regular expressions.

.matches("\\d+")

Should do it.

FYI the String.matches() method must match the whole input to return true.


Even in languages like perl, the slashes are not part of the regex; they are delimiters - part if the application code, nothing to do with the regex

like image 23
Bohemian Avatar answered Oct 22 '22 20:10

Bohemian