正则表达式:一行字符串只包含浮点数和制表符/空格 [英] regular expression: a line of string contains only float numbers and tab/spaces
本文介绍了正则表达式:一行字符串只包含浮点数和制表符/空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
一行字符串的正则表达式是什么,只包含用空格或制表符分隔的浮点数.浮点数可以是负数,例如 -999.999
what the regular expression of a line of string containing ONLY float numbers separated with spaces or tabs. The float number can be negative, like -999.999
推荐答案
让我们想出一个浮点数的正则表达式,然后看看我们能做些什么.
Let's come up with a regex for a float, and then see what we can do about the rest.
浮点数是:
- 一个可选的负号
- 后跟多个数字
- 后跟一个可选的小数点,然后是更多的数字
- 后面是e"
- 后跟多个数字(带有可选符号).
把它们放在一起,我们得到:
Put that together, and we get:
/-?[0-9]+(\.[0-9]+)?([Ee][+-]?[0-9]+)?/
现在,这很松散,但如果你想把它收紧一点,你可以调整它.现在,对于其中有空格的任意数量,这非常简单:
Now, this is pretty loose, but you can tweak it if you want to tighten it up a little. Now, for any number of these with spaces in between, it's pretty trivial:
/^(F\s+)+$/
把它们放在一起,我们最终得到:
Put it all together, we end up with:
/^(-?[0-9]+(\.[0-9]+)?([Ee][+-]?[0-9]+)?\s+)+$/
这篇关于正则表达式:一行字符串只包含浮点数和制表符/空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文