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)
Case 1: x 1.8 y 1.0 value9.99999986991104E14
Case 2: x 1 y 1 value9.99999986991104E14
for x,y and value. 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).
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. - Since the value String is fixed, I included it in the regex.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.