java - Regex to split nested coordinate strings -
I have a string of format "[(1, 2), (2, 3), (3 , 4)] "
, with an arbitrary number of elements, I'm trying to divide it by commas that separate the coordinates, i.e., (1, 2)
, < Code> (2, 3) , and to get (3, 4)
.
Can I do this in Java Reggae? I'm totally communicating but it is quite powerful to hope for Java reggae. If it is not, can you give an alternative suggestion?
You can use it for
string string = " [(1, 2), (2, 3), (3, 4)]; String = string.substring (1, string.length () - 1); // Get rid of strings string [] parts = string. Split ("(? & Lt; = \\)) (, \\ s *) (? = \\ ()"); For (string part: parts) {part = part.substring (1, part.length () - 1); // Get rid of brackets string [] coords = part.split (", \\ s *"); Int x = integer.center (quotes [0]); Int y = integer.centre (coars [1]); System.out.printf ("x =% d, y =% d \ n", x, y); }
(? & Lt; = \\))
means it should be before )
. (? = \\ ()
means that it should be suceded by (
. <>> ), that means it It should be distinguished in any place at ,
and beyond. \\
Here only is to avoid regex-specific characters.
Updates , according to the comments, you can actually do other ways also String string = "[(1, 2), (2, 3), (3, 4)]"; [Int] I = l;; parts = string
divided by ("\\ D."); for (int i = 1; i & lt; parts.lab; i + = 3) {int x = Integer.partyant (part [i]); int y = integer.centrate (parts [i + 1]); system.out.printf ("x =% d, y =% d \ n", x, y) ;}
here \\ D
means that it has any non -digit ( \\ d
stands for points). .
After that it means that it should end any empty matches, although I accept it Would that I do not end up how empty match is sure to before points I am still not a trained regex guru Hey, Bart, you can do it better?
After all, it is better to use parser for it.
Comments
Post a Comment