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

  • SEARCH
  • Home
  • 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 9158661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T13:18:04+00:00 2026-06-17T13:18:04+00:00

I am a beginner of Java Programming language. When I input (1,2) into the

  • 0

I am a beginner of Java Programming language.

When I input (1,2) into the console (brackets included), how can I write the code to extract the first and the second number using RegEx?

If there is no such expression to extract the first/second number within the brackets, I will have to change the way of inputing coordinates to x,y without the brackets and that should be a lot easier to extract numbers to be used.

  • 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-06-17T13:18:05+00:00Added an answer on June 17, 2026 at 1:18 pm

    Try this code:

    public static void main(String[] args) {
        String searchString = "(7,32)";
        Pattern compile1 = Pattern.compile("\\(\\d+,");
        Pattern compile2 = Pattern.compile(",\\d+\\)");
        Matcher matcher1 = compile1.matcher(searchString);
        Matcher matcher2 = compile2.matcher(searchString);
        while (matcher1.find() && matcher2.find()) {
            String group1 = matcher1.group();
            String group2 = matcher2.group();
            System.out.println("value 1: " + group1.substring(1, group1.length() - 1 ) + " value 2: " + group2.substring(1, group2.length() - 1 ));
        }
    }
    

    Not that I think regex is the best to use here. If you know the input will be in the form of: (number, number), I would first get rid of brackets:

    stringWithoutBrackets = searchString.substring(1, searchString.length()-1) 
    

    and than tokenize it with split

    String[] coordiantes = stringWithoutBrackets.split(",");
    

    Looked through Regex API and you can also do something like this:

    public static void main(String[] args) {
        String searchString = "(7,32)";
        Pattern compile1 = Pattern.compile("(?<=\\()\\d+(?=,)");
        Pattern compile2 = Pattern.compile("(?<=,)\\d+(?=\\))");
        Matcher matcher1 = compile1.matcher(searchString);
        Matcher matcher2 = compile2.matcher(searchString);
        while (matcher1.find() && matcher2.find()) {
            String group1 = matcher1.group();
            String group2 = matcher2.group();
            System.out.println("value 1: " + group1 + " value 2: " + group2);
        }
    }
    

    The main change is that I used (?<==\)), (?=,), (?<=,), (?=\)), to search for brackets and commas but not caputre them. But I really think its an overkill for this task.

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

Sidebar

Related Questions

I am beginner in Java Programming. How can i show my output from console
I am a beginner in programming (learning Java). I am trying to write a
I am a beginner in Java Programming. My code encrypts data extracted from a
I'm a beginner in Java programming language, and I have a problem that I
I am beginner with Java, and I would like to write some code like
I'm a total beginner with my first programming assignment in Java. For our programming
I am a beginner to android. in java and other programming languages we get
OK guys, up till now (and since I'm a beginner) I was programming Java
I am a beginner when it comes to programming with Android/Java, and I am
Hello :) I am beginner in Java Swing and I can't google solution for

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.