简体   繁体   English

唯一整数字符串的Java正则表达式是什么?

[英]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 / : 在Java正则表达式中,您不使用分隔符/

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. 由于String.matches() (或Matcher.matcher() )强制整个字符串与模式匹配以返回true ,因此^$实际上是多余的,可以在不影响结果的情况下删除。 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. 与JavaScript,PHP(PCRE)或Perl相比,这有点不同,其中“匹配”意味着在目标字符串中查找与模式匹配的子字符串。

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: 要限制正好是 4位数:

"\\d{4}"

As others have already said, java doesn't use delimiters. 正如其他人已经说过的那样,java不使用分隔符。 The string you're trying to match doesn't need the trailing slashes, so instead of /^\\\\d+$/ your string should've been ^\\\\d+$ . 你想要匹配的字符串不需要尾部斜杠,所以代替/^\\\\d+$/你的字符串应该是^\\\\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+$ 当然,在Java中你需要一个双反斜杠而不是常规的反斜杠,所以匹配上述正则表达式的Java字符串是^-?\\\\d+$


NOTE: The ^$ (string start/end) characters aren't needed if you're using .matches() : 注意:如果您使用.matches() ,则不需要^$ (字符串开头/结尾)字符:

Welcome to Java's misnamed .matches() method... It tries and matches ALL the input. 欢迎使用Java的错误名称.matches()方法...它尝试并匹配所有输入。 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 即使它是可选的,我仍然将它包含在正则表达式可读性中,就像在其他情况下默认情况下你不匹配整个字符串一样(大多数情况下,如果你不使用.matches() ) d使用这些字符


To match the opposite: 相反的情况:

^\D+$

The \\D is everything that is not a digit. \\D是不是数字的一切。 The \\D (Non digits) negates \\d (digits). \\D (非数字)否定\\d (数字)。

Integer regex on regex101 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 \\. 当然用Java代替\\d\\. you'd have double backslashes as the above example does. 你有双反斜杠,如上例所示。

Doubles regex on regex101 regex101上的双重正则表达式

Java doesn't use slashes to delimit regular expressions. Java不使用斜杠来分隔正则表达式。

.matches("\\d+")

Should do it. 应该这样做。

FYI the String.matches() method must match the whole input to return true . FYI String.matches()方法必须匹配整个输入才能返回true


Even in languages like perl, the slashes are not part of the regex; 即使在像perl这样的语言中,斜杠也不是正则表达式的一部分; 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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