使用 matcher() 匹配字符串中的小数
我有一个关于匹配器的问题。目前我正在尝试读取一个字符串并将所有数字存储到一个数组中。我的问题是,你如何尝试匹配整数和小数?
我有一个双打数组,名为:
double[] thisArray = new double[20];
在这个数组中,我试图存储从字符串中提取的所有数字。
匹配器温度= Pattern.compile("(\d+)").matcher(x);
这就是我的匹配器功能。但这仅匹配整数。我想匹配整数和小数,如(5.2)。但我该怎么做呢?我希望能够在字符串中输入整数和小数。
任何帮助将不胜感激。谢谢!
I have a question regarding the matcher. Currently I am trying to read a string and store all the digits into an array. My question is, how do you try to match both integers and decimals?
I have an array of doubles called:
double[] thisArray = new double[20];
Into this array, i am trying to store all the numbers I extract from the string.
Matcher temp =
Pattern.compile("(\d+)").matcher(x);
That is my function for the matcher. But this only matches integers. I want to match both integers and decimals like (5.2). But how do I do this? I want to be able to enter in both integers and decimals into my string.
Any help would be appreciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这将处理整数和小数:-
This will handle both integer and decimals:-
短语
\d+
将匹配一串数字。那么在两者之间添加一个点怎么样?(\d+)|(\d+|\.\d+)
The phrase
\d+
will match a string of numbers. So what about adding a dot between two of them?(\d+)|(\d+|\.\d+)