I want to find all the negative floating point numbers in a string and store them in an array. I think my regex is correct, but something is wrong with my method.
Pattern pattern = Pattern.compile("[-]?[0-9]*[.][0-9]+$");
String[] results = pattern.split("|AAA--A A05_#A| |-999.999| |-55.7|");
Your regex anchors the match to the end of the string, which isn’t what you want.
Likewise,
Pattern.splitdoesn’t do what you want. Here’s some sample code to get you going:This prints:
Obviously you could add to a list or something similar within the
whileloop. I don’t know of any method which returns a collection of all the matches, but you could easily write a utility method to do that yourself, based on code like the above.EDIT: As noted in comments, if you only want to find negative values, the
-shouldn’t be optional (and it doesn’t need to be in a set, either – just-?would have been fine).