[英]Java : Extract numbers from a string
I was trying to extract my data from a string by using regular expression. 我试图使用正则表达式从字符串中提取数据。
My data looks like: 我的数据如下:
12 170 0.11918
170 12 0.11918
12 182 0.06361
182 12 0.06361
12 198 0.05807
198 12 0.05807
12 242 0.08457
242 12 0.08457
11 30 0.08689
30 11 0.08689
The problems here are the different number of whitespace between two numbers. 这里的问题是两个数字之间的空白数不同。
All in all i want to extract from each line two Integers and one Double. 总之,我想从每行中提取两个Integer和一个Double。 Therefore i tried to use regular expressions.
因此,我尝试使用正则表达式。
Pattern p = Pattern.compile("(([0-9]+.[0-9]*)|([0-9]*.[0-9]+)|([0-9]+))");
Matcher m = p.matcher(" 6 7781 0.01684000");
while (m.find()) {
System.out.println(m.group(0));
}
I now my regular expression doesn't work. 我现在的正则表达式不起作用。 Has anyone some help for a suitable regular expression therefore i can work with the data or any other help for me?
有没有人对适当的正则表达式有所帮助,因此我可以为我处理数据或其他任何帮助?
why not read each line and do a line.trim().split("\\\\s+")
? 为什么不阅读每一行并执行
line.trim().split("\\\\s+")
? If your project has already used guava, the Splitter
could be used too. 如果您的项目已经使用了番石榴,则也可以使用
Splitter
。
I recommend using a Scanner
. 我建议使用
Scanner
。
Scanner scanner = new Scanner(line);
scanner.useDelimiter(" ");
int int1 = scanner.nextInt()
int int2 = scanner.nextInt()
double double1 = scanner.nextDouble()
check http://txt2re.com/index-java.php3?s=%2012%20170%200.11918&11&5&12&4&13&1 检查http://txt2re.com/index-java.php3?s=%2012%20170%200.11918&11&5&12&4&13&1
you're probably interested in the int1, int2 and float1 below 您可能对下面的int1,int2和float1感兴趣
public static void main(String[] args)
{
String txt=" 12 170 0.11918";
String re1="(\\s+)"; // White Space 1
String re2="(\\d+)"; // Integer Number 1
String re3="(\\s+)"; // White Space 2
String re4="(\\d+)"; // Integer Number 2
String re5="(\\s+)"; // White Space 3
String re6="([+-]?\\d*\\.\\d+)(?![-+0-9\\.])"; // Float 1
Pattern p = Pattern.compile(re1+re2+re3+re4+re5+re6,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(txt);
if (m.find())
{
String ws1=m.group(1);
String int1=m.group(2);
String ws2=m.group(3);
String int2=m.group(4);
String ws3=m.group(5);
String float1=m.group(6);
System.out.print("("+ws1.toString()+")"+"("+int1.toString()+")"+"("+ws2.toString()+")"+"("+int2.toString()+")"+"("+ws3.toString()+")"+"("+float1.toString()+")"+"\n");
}
}
Try this: 尝试这个:
([\\d.]+)
- This will get all strings containing only digits or periods (.). ([\\d.]+)
-这将获取所有仅包含数字或句点(。)的字符串。
Edit: 编辑:
I see you're wanting three groups out of a line. 我看到您想将三个小组排成一行。 This instead, will help by ignoring white space, and grabbing the three groups of numbers.
相反,这将有助于忽略空白,并抓住三组数字。 The leading
^
and trailing $
ensure that you're only matching on a single line. 前导
^
和尾随$
确保您只匹配一行。
^\\s*?([\\d.]+)\\s*([\\d.]+)\\s*?([\\d.]+)\\s*?$
Something like this (fix up the float part as needed) - 这样的事情(根据需要修复浮动部分)-
# raw: (?m)^\h*(\d+)\h+(\d+)\h+(\d*\.\d+)
# quoted: "(?m)^\\h*(\\d+)\\h+(\\d+)\\h+(\\d*\\.\\d+)"
(?m) # Multi-line modifier
^ # BOL
\h* # optional, horizontal whitespace
( \d+ ) # (1), int
\h+ # required, horizontal whitespace
( \d+ ) # (2), int
\h+ # required, horizontal whitespace
( \d* \. \d+ ) # (3), float
String s = " 12 170 0.11918\n" + "170 12 0.11918 \n"
+ " 12 182 0.06361\n" + "182 12 0.06361 \n"
+ " 12 198 0.05807\n" + "198 12 0.05807 \n"
+ " 12 242 0.08457\n" + "242 12 0.08457 \n"
+ " 11 30 0.08689\n" + " 30 11 0.08689 \n";
String[] lines = s.split("\\n");
for( String line : lines ) {
Scanner scan = new Scanner(line);
scan.useDelimiter("\\s+");
scan.useLocale(Locale.ENGLISH);
System.out.println(scan.nextInt());
System.out.println(scan.nextInt());
System.out.println(scan.nextDouble());
}
I would use a Scanner for this problem. 我将使用扫描仪解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.