简体   繁体   中英

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

我正在尝试if (nuevo_precio.getText().matches("/^\\\\d+$/"))但到目前为止得不到好结果......

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}"

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

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

YOu can also go for negation to check whether a number is a pure numeric or not.

Pattern pattern = Pattern.compile(".*[^0-9].*");
for(String input: inputs){
           System.out.println( "Is " + input + " a number : "
                                + !pattern.matcher(input).matches());
}
    public static void main(String[] args) {
    //regex is made to allow all the characters like a,b,...z,A,B,.....Z and 
    //also numbers from 0-9.
    String regex = "[a-zA-z0-9]*";

    String stringName="paul123";
    //pattern compiled   
    Pattern pattern = Pattern.compile(regex);

    String s = stringName.trim();

    Matcher matcher = pattern.matcher(s);

    System.out.println(matcher.matches());
    }

正则表达式适用于数字,而不是整数:

Integer.MAX_VALUE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM