简体   繁体   English

浮点数/双精度数的正则表达式

[英]Regular expression for float/double numbers

I've been trying to retrieve a string and 2 decimal numbers from a sentence, but the decimal part hasn't been working well. 我一直在尝试从一个句子中检索一个字符串和2个十进制数字,但是小数部分一直无法正常工作。

The sentence I have is on this format: AMBIENT_CAVE 0.5 1 So it would be word (sometimes containing _ ) then 2 numbers that could be either integer (1 or 0) or a decimal between 0 and 1. 我的句子采用以下格式: AMBIENT_CAVE 0.5 1因此,它将是word (有时包含_ )然后是2个数字,它们可以是整数(1或0)或0到1之间的十进制数。

My current regular expression is: (\\w+) ([+-]?([0-9]*[.])?[0-9]+) ([+-]?([0-9]*[.])?[0-9]+) Which kinda works, but using the sentence example from above group 3 would be 0. . 我当前的正则表达式为: (\\w+) ([+-]?([0-9]*[.])?[0-9]+) ([+-]?([0-9]*[.])?[0-9]+)哪一种有效,但使用上面第3组中的句子示例为0.

Results: 结果:

在此处输入图片说明

I only want AMBIENT_CAVE , 0.5 and 1 . 我只想要AMBIENT_CAVE0.51
Anyone know how to do it? 有人知道怎么做吗?

Add a ?: to any group you don't want to capture: 在您不想捕获的任何组中添加?:

(\w+) ([+-]?(?:[0-9]*[.])?[0-9]+) ([+-]?(?:[0-9]*[.])?[0-9]+)
             ^                           ^

You can try this one. 你可以试试这个。 Please provide more examples of available strings in order to optimize an expression 请提供更多可用字符串的示例,以优化表达式

  (\w+)\s([\d\.]+)\s([\d\.]+)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM