仅整数字符串的 Java 正则表达式是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16331423/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What's the Java regular expression for an only integer numbers string?
提问by diegoaguilar
I'm trying with if (nuevo_precio.getText().matches("/^\\d+$/"))
but got not good results so far...
我正在尝试,if (nuevo_precio.getText().matches("/^\\d+$/"))
但到目前为止效果不佳......
采纳答案by nhahtdh
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. 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.
由于String.matches()
(or Matcher.matcher()
) 强制整个字符串与模式匹配 return true
,^
和$
实际上是多余的,可以删除而不影响结果。与 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 exactly4 digit numbers:
要限制正是4个数字:
"\d{4}"
回答by Bohemian
Java doesn't use slashes to delimit regular expressions.
Java 不使用斜杠来分隔正则表达式。
.matches("\d+")
Should do it.
应该做。
FYI the String.matches()
method must match the wholeinput to return true
.
仅供参考,该String.matches()
方法必须匹配整个输入才能返回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
即使在像 perl 这样的语言中,斜线也不是正则表达式的一部分。它们是分隔符- 如果是应用程序代码的一部分,与正则表达式无关
回答by Rahul
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());
}
回答by NonameSL
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+$
.
正如其他人已经说过的,java 不使用分隔符。您尝试匹配的字符串不需要尾部斜杠,因此/^\\d+$/
您的字符串应该是^\\d+$
.
Now I know this is an old question, but most of the people here forgot something very important. The correctregex 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. Unfortunately, other languages have followed suit :(- Taken from this answer
欢迎使用 Java 的错误命名
.matches()
方法...它尝试并匹配所有输入。不幸的是,其他语言也纷纷效仿:(- 取自这个答案
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()
)您会使用这些字符
To match the opposite:
匹配相反的:
^\D+$
The \D
is everything that is not a digit. The \D
(Non digits) negates \d
(digits).
该\D
是一切,这是不是一个数字。该\D
(非数字),通过则无效\d
(位)。
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.
当然,在 Java 中\d
,\.
你会像上面的例子那样使用双反斜杠。
回答by dyhim
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());
}
回答by bilama
Regex just works for numbers, not integers :
正则表达式只适用于数字,而不适用于整数:
Integer.MAX_VALUE