Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 600307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:35:30+00:00 2026-05-13T16:35:30+00:00

I have a String of the format [(1, 2), (2, 3), (3, 4)] ,

  • 0

I have a String of the format "[(1, 2), (2, 3), (3, 4)]", with an arbitrary number of elements. I’m trying to split it on the commas separating the coordinates, that is, to retrieve (1, 2), (2, 3), and (3, 4).

Can I do it in Java regex? I’m a complete noob but hoping Java regex is powerful enough for it. If it isn’t, could you suggest an alternative?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:35:30+00:00Added an answer on May 13, 2026 at 4:35 pm

    You can use String#split() for this.

    String string = "[(1, 2), (2, 3), (3, 4)]";
    string = string.substring(1, string.length() - 1); // Get rid of braces.
    String[] parts = string.split("(?<=\\))(,\\s*)(?=\\()");
    for (String part : parts) {
        part = part.substring(1, part.length() - 1); // Get rid of parentheses.
        String[] coords = part.split(",\\s*");
        int x = Integer.parseInt(coords[0]);
        int y = Integer.parseInt(coords[1]);
        System.out.printf("x=%d, y=%d\n", x, y);
    }
    

    The (?<=\\)) positive lookbehind means that it must be preceded by ). The (?=\\() positive lookahead means that it must be suceeded by (. The (,\\s*) means that it must be splitted on the , and any space after that. The \\ are here just to escape regex-specific chars.

    That said, the particular String is recognizeable as outcome of List#toString(). Are you sure you’re doing things the right way? 😉

    Update as per the comments, you can indeed also do the other way round and get rid of non-digits:

    String string = "[(1, 2), (2, 3), (3, 4)]";
    String[] parts = string.split("\\D.");
    for (int i = 1; i < parts.length; i += 3) {
        int x = Integer.parseInt(parts[i]);
        int y = Integer.parseInt(parts[i + 1]);
        System.out.printf("x=%d, y=%d\n", x, y);
    }
    

    Here the \\D means that it must be splitted on any non-digit (the \\d stands for digit). The . after means that it should eliminate any blank matches after the digits. I must however admit that I’m not sure how to eliminate blank matches before the digits. I’m not a trained regex guru yet. Hey, Bart K, can you do it better?

    After all, it’s ultimately better to use a parser for this. See Huberts answer on this topic.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 298k
  • Answers 298k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer For python2.6 with open("file1") as infile: with open("file2","w") as outfile:… May 13, 2026 at 7:30 pm
  • Editorial Team
    Editorial Team added an answer are you sure that your entities are in the 2nd… May 13, 2026 at 7:30 pm
  • Editorial Team
    Editorial Team added an answer Here are the tools I'm aware of (and just aware):… May 13, 2026 at 7:30 pm

Related Questions

I have a table containing reports and the date/time they were created. I'd like
Say, I have a string that I need to verify the correct format of;
I have been given a file that I would like to extract the useful
Summary: How can I determine TFS source control configuration for a local folder. Explanation:
I am having an issue converting type. I was trying code like this (minimal,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.