0xC0DED00D
0xC0DED00D

Reputation: 20348

Converting String into Matrix position

There is a String which is given by a user which is in the format "{{row1,col1},{row2,col2},{row3,col3}}". These coordinates are certain locations which are required to be fill in a matrix i.e. changing the matrix values when the same coordinate is found.

The String could be of any length, but we'll know how many coords it'll have.

The problem is I can't find any fast solution to convert this string into locations. I am trying to use StringTokenizer class, but there're too many separators.

Can you help me find a better solution for this problem.

Thanks in advance.

Upvotes: 1

Views: 177

Answers (1)

KV Prajapati
KV Prajapati

Reputation: 94645

Try this,

   String str="{{row1,col1},{row2,col2},{row3,col3}}";

   Pattern pattern = Pattern.compile("(\\w+,\\w+)+");
   Matcher matcher = pattern.matcher(str);

   while (matcher.find()) 
    {
        String search=matcher.group();
        String []ar=search.split(",");
        System.out.println(ar[0] + " " + ar[1]);
    }

Upvotes: 2

Related Questions