简体   繁体   English

正则表达式与浮点值不匹配

[英]Regular expression not matching float value

I am trying to write a regular expression for these 2 lines of String 我试图为这两行字符串写一个正则表达式

txt="[0, 0, 1.8, 1.0] value = 9.99999986991104E14";
txt="[0, 0, 1, 1] value = 9.99999986991104E14";

String re1=".*?";   // Non-greedy match on filler
String re2="\\d+";  // Uninteresting: int
String re3=".*?";   // Non-greedy match on filler
String re4="\\d+";  // Uninteresting: int
/*
* x 3rd value
*/
String re5=".*?";   // Non-greedy match on filler
String re6="(\\d+)";    // Integer Number 1
/*
* y 4 th value
*/
String re7=".*?";   // Non-greedy match on filler
String re8="(\\d+)";    // Integer Number 2
/*
* value
*/
String re9=".*?";   // Non-greedy match on filler
String re10="([+-]?\\d*\\.\\d+)(?![-+0-9\\.])"; // Float 1
String re11="((?:[a-z][a-z]*[0-9]+[a-z0-9]*))"; // Alphanum 1

Pattern p = Pattern.compile(re1+re2+re3+re4+re5+re6+re7+re8+re9+re10+re11,Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
Matcher m = p.matcher(txt);

if (m.find())
{
    String x = m.group(1);
    String y = m.group(2);
    String value = m.group(3)+m.group(4);
    System.out.print("x "+x.toString()+" y "+y.toString()+" value"+value.toString()+"\n");
}

These regular expressions give me the the following result 这些正则表达式给我以下结果

case 1: x 1 y 8 value9.99999986991104E14
Case 2: x 1 y 1 value9.99999986991104E14

However, I had expected the results to be (case 1) 但是,我曾预期结果会是(案例1)

 Case 1: x 1.8 y 1.0 value9.99999986991104E14
 Case 2: x 1 y 1 value9.99999986991104E14

for x,y and value. x,y和值。 I need to match both the integer and the float value. 我需要同时匹配整数和浮点值。

Am I doing anything wrong with my regular expression? 我的正则表达式有什么问题吗?

Looking at the values for re6 and re8 , you are only looking at non decimal values (the dot is not included). 查看re6re8的值,您仅查看非十进制值(不包括点)。

I suggest including the dot with the following changes 我建议对点进行以下更改

String re6 = "([.\\d]+)";

and

String re8 = "([.\\d]+)";

Edit for addition on the "value" part: 编辑 “值”部分上的加法:

For the last Group, where you are trying to get number string, I would do it like this: 对于最后一个试图获取数字字符串的组,我会这样做:

String re10 = "value ="; // Searching for fixed String value
String re11 = "[ ]*"; // Looking for whitespaces
String re12 = "([.E\\d]+)";// Looking for A String containing: Numbers/Dots/Letter-E at with at least one match

One Group to match both cases: Including E or not, Containing dot or not. 一组可以同时满足两种情况:是否包含E,是否包含点。 - Since the value String is fixed, I included it in the regex. -由于字符串的值是固定的,因此我将其包含在正则表达式中。

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

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