java 标记百分比的正则表达式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30072184/
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
Regular Expression for Percentage of marks
提问by Nishant123
I am trying to create a regex that matches percentage for marks
我正在尝试创建一个匹配标记百分比的正则表达式
For example if we consider few percentages
例如,如果我们考虑几个百分比
1)100%
2)56.78%
3)56 78.90%
4)34.6789%
The matched percentages should be
匹配的百分比应该是
100%
56.78%
34.6789%
I have made an expression "\\d.+[\\d]%"
but it also matches for 56 78.90%
which I don't want.
我做了一个表达式,"\\d.+[\\d]%"
但它也匹配了56 78.90%
我不想要的。
If anyone knows such expression please share
如果有人知道这样的表达请分享
回答by vks
\d+(?:\.\d+)?%
This should do it for you.
这应该为你做。
For more stringent test use,
对于更严格的测试用途,
\b(?<!\.)(?!0+(?:\.0+)?%)(?:\d|[1-9]\d|100)(?:(?<!100)\.\d+)?%
See demo.
见演示。
回答by Mena
You haven't double-escaped your dot, which means it's a wildcard for any character, including whitespace.
您没有对点进行双重转义,这意味着它是任何字符的通配符,包括空格。
Use something like:
使用类似的东西:
┌ integer part - any 1+ number of digits
| ┌ dot and decimal part (grouped)
| |┌ double-escaped dot
| || ┌ decimal part = any 1+ number of digits
| || | ┌ 0 or 1 greedy quantifier for whole group
| || | |
"\d+(\.\d+)?%"
For instance:
例如:
String[] inputs = { "100%", "56.78%", "56 78.90%", "34.6789%" };
Matcher m = null;
for (String s: inputs) {
m = p.matcher(s);
if (m.find())
System.out.printf("Found: %s%n", m.group());
}
Output
输出
Found: 100%
Found: 56.78%
Found: 78.90%
Found: 34.6789%
Note
笔记
This still matches the 3rd input, but only the last part.
这仍然匹配第三个输入,但只匹配最后一部分。
If you want the 3rd input to just not match, you can surround your pattern with input boundaries, such as ^
for start of input, and $
for end of input.
如果您希望第三个输入不匹配,您可以用输入边界包围您的模式,例如^
输入开始和输入$
结束。
That would become: "^\\d+(\\.\\d+)?%$"
那将变成: "^\\d+(\\.\\d+)?%$"
Or, you can simply invoke Matcher#matches
instead of Matcher#find
.
或者,您可以简单地调用Matcher#matches
而不是Matcher#find
.
Next step
下一步
You may want to do somethingwith the numerical value you're retrieving.
您可能想要对正在检索的数值做一些事情。
In this case, you can surround your pattern with a group ("(\\d+(\\.\\d+)?)%"
) and invoke either Double.parseDouble
or new BigDecimal(...)
on your back-reference:
在这种情况下,您可以用组 ( "(\\d+(\\.\\d+)?)%"
)包围您的模式,并在您的反向引用中调用Double.parseDouble
或new BigDecimal(...)
:
Double.parseDouble(m.group(1))
new BigDecimal(m.group(1))
Double.parseDouble(m.group(1))
new BigDecimal(m.group(1))
回答by Saurabh Gupta
^((100)|(\d{1,2}(.\d*)?))%$
^((100)|(\d{1,2}(.\d*)?))%$
Check this regular expression here: https://regex101.com/r/Ou3mJI/2
在此处检查此正则表达式:https: //regex101.com/r/Ou3mJI/2
You can use this regular expression. It is valid for:
您可以使用此正则表达式。它适用于:
- 0 to 100 inclusive
- With and without decimal places
- 0 到 100(含)
- 有和没有小数位
Below are valid values:
以下是有效值:
100% is valid
99.802% is valid
98.7% is valid
57% is valid
0% is valid
100% 有效
99.802% 有效
98.7% 有效
57% 有效
0% 有效
This regular expression invalidates below values:
此正则表达式使以下值无效:
- Negative numbers
- Number > 100
- Number with spaces
- 负数
- 数量 > 100
- 带空格的数字
Invalid value examples:
无效值示例:
-1%
99.989%
101%
56 78.90%
-1%
99.989%
101%
56 78.90%
Hope this will help!
希望这会有所帮助!
回答by Alvin Magalona
The RegEx \\d+(\\.?\\d+)?%
would work.
正则表达式\\d+(\\.?\\d+)?%
会起作用。