正则表达式匹配由空格分隔的数字值,但整个字符串不以空格结尾 [英] Regex to match digit values separated by space, but whole string doesn't end with space

查看:102
本文介绍了正则表达式匹配由空格分隔的数字值,但整个字符串不以空格结尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了一个 EditText,它允许输入以空格分隔的价格.价格可以是整数和小数值.

I implement an EditText that allows to input prices separated by a space. The prices can be both integer and decimal values.

例如:

3900 156.2 140.38 200 10

3900 156.2 140.38 200 10

所以,我找到了以下正则表达式:

So, I've found the following regex:

(\d+([.]\d{1,2})? )+

但它只适用于末尾的空格.(我知道这绝对值得期待)

But it works only with a space at the end. (I know it's absolutely expectable)

是否可以让这个正则表达式忽略输入字符串末尾的空格?

Is it possible to make this regex to ignore a space at the end of input string?

或者您可以提出其他建议?

Or maybe you can suggest smth else?

谢谢.

推荐答案

您也可以使用(几乎)您提供的相同模式:

You can also use (almost) the same pattern you provided:

(\d+([.]\d{1,2})? )*(\d+([.]\d{1,2})?)

这不如 Tim 的回答灵活(例如:如果您在数字之间放置多个空格,则会失败等),但无论如何它可能很有用.

This is less flexible than Tim's answer (like: if you put more than one space between numbers it will fail, etc) but it might be useful anyway.

这篇关于正则表达式匹配由空格分隔的数字值,但整个字符串不以空格结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆