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 9069051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T17:19:43+00:00 2026-06-16T17:19:43+00:00

Pattern p = Pattern.compile((.+)\\s(.+)\\s(.?)$); Matcher m = p.matcher(line); System.out.println(Method:); System.out.println(m.group(1)); System.out.println(m.group(2)); The code above

  • 0
Pattern p = Pattern.compile("(.+)\\s(.+)\\s(.?)$");
Matcher m = p.matcher(line);
System.out.println("Method:");  
System.out.println(m.group(1));
System.out.println(m.group(2));

The code above is my code I use to split this string:

method(public, static, void) main(String[] args){

I want to be able to get method(public, static, void) in a string and main(String[] args) in another string. I don’t want the last { and there may be be spaces between the ) and the {.

My code so far does this:

Method:
method(public, static, void) main(String[]
args){ 

I am not any good at regex. (My code doesn’t currently handle the last {, but I can fix this.) The problem is that I cannot get the line to split how I want it to.

  • 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-16T17:19:44+00:00Added an answer on June 16, 2026 at 5:19 pm

    You don’t need Pattern and Matcher here. You should rather use String#split() method to split your string on ), and get first two elements of your array: –

    String str = "method(public, static, void) main(String[] args){";
    
    String[] parts = str.split("\\)");
    
    System.out.println(parts[0]);  // method(public, static, void
    System.out.println(parts[1]);  // main(String[] args
    

    But, the problem that you can see is, your strings won’t contain the last ) in them.

    To include the delimiter in the array elements, you can use look-behind on your regex: – "(?<=\\))". Now, this will split your string on empty character preceded by ).

    This is what you need: –

    String[] parts = str.split("(?<=\\))");  // split on empty character after )
    System.out.println(parts[0]);
    System.out.println(parts[1]);
    

    output: –

    method(public, static, void)
     main(String[] args)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code : Pattern pattern = Pattern.compile(a?); Matcher matcher = pattern.matcher(ababa); while(matcher.find()){ System.out.println(matcher.start()+[+matcher.group()+]+matcher.end()); }
Pattern pattern = Pattern.compile(a?); Matcher matcher = pattern.matcher(a); while(matcher.find()){ System.out.println(matcher.start()+[+matcher.group()+]+matcher.end()); } Output : 0[a]1
This code does not return the correct result: Pattern p=Pattern.compile(^[y]{1,4}$|^[m]{1,4}$|^[d]{1,4}$); String text1=yyyy; String text2=mmm;
Pattern pattern = Pattern.compile(<a>([a-zA-Z]+)</a>) Matcher matcher = pattern.matcher(<a>Text</a>); matcher.find() String str = matcher.group(); I
File file = new File(file-type-string-i-want-2000-01-01-01-01-01.conf.gz); Matcher matcher = pattern.compile(\\-(.*)\\-\\d{4})).matcher(fileName); StringBuilder sb = new StringBuilder();
In this statement, taken from the Pagerank source code: Pattern.compile(\\[.+?\\]); What does the pattern
This is our code: import java.io.File; import java.io.FileNotFoundException; import java.util.*; import java.util.regex.Matcher; import java.util.regex.Pattern;
public static void main(String[] args) { Pattern compile = Pattern .compile([0-9]{1,}[A-Za-z]{1,}|[A-Za-z][0-9]{1,}|[a-zA-Z][a-zA-Z0-9\\.\\-_/#]{2,}|[0-9]{3,}[A-Za-z][a-zA-Z0-9\\.\\-_/#]*|[0-9][0-9\\-]{4,}|[0-9][0-9\\-]{3,}[a-zA-Z0-9\\.\\-_/#]+); Matcher matcher =
Pattern pattern = Pattern.compile(^[a-z]+$); String string = abc-def; assertTrue( pattern.matcher(string).matches() ); // obviously fails
I have a below peace of code : Pattern p = Pattern.compile(^\\d|^\\W|^\\s); Matcher m

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.