Reputation: 2059
I want to match regex pattern in java for particular text. In that while matching I need to find matches in reverse order.
Example:
Regex Pattern -- [([^]]*])
Input String -- [blue][red][green]
Output -- 1st match --->[blue]
2nd match --->[red]
3rd match --->[green]
But I'm expecting match to be in reverse order.
Expected output -- 1st match --->[green]
2nd match --->[red]
3rd match --->[blue]
Please help how to form the regex to achieve the expected output.
Upvotes: 1
Views: 1286
Reputation: 12367
Assuming you have complied regex date"
ArrayList<String> strings = new ArrayList<>();
Matcher foo = date.matcher("foo");
while (foo.matches()) {
strings.add(foo.group());
}
Collections.reverse(strings);
Upvotes: 1
Reputation: 606
Reverse regular expression in Java discusses the topic and I posted a solution there, added here for convenience.
If anyone is interested in a Java solution, I implemented a library to do just that. https://github.com/vsch/reverse-regex.
Handles all valid Java regex constructs and provides utility classes to wrap pattern, matcher and input for reverse searches to handle all need mappings and reversals.
Upvotes: 0