![](/img/trans.png)
[英]extract a double or integer from a string using regular expression in java?
[英]Regular Expression to split double and integer numbers in a string
我需要一个正则表达式来分隔字符串的整数和双元素,如下例所示:
String input = "We have the number 10 and 10.3, and i want to split both";
String[] splitted = input.split(/*REGULAR EXPRESSION*/);
for(int i=0;i<splitted.length;i++)
System.out.println("[" + i + "]" + " -> \"" + splitted[i] + "\"");
输出将是:
有人能帮我吗? 我会很感激。
您需要将这些块与以下内容匹配 :
\D+|\d*\.?\d+
请参阅正则表达式演示
细节 :
\\D+
- 除数字以外的1个或更多字符 |
- 要么 \\d*\\.?\\d+
- 一个简单的整数或浮点数(可能会增强到[0-9]*[.]?[0-9]+(?:[eE][-+]?[0-9]+)?
见来源 ) Java演示 :
String s = "We have the number 10 and 10.3, and i want to split both";
Pattern pattern = Pattern.compile("\\D+|\\d*\\.?\\d+");
Matcher matcher = pattern.matcher(s);
List<String> res = new ArrayList<>();
while (matcher.find()){
res.add(matcher.group(0));
}
System.out.println(res);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.