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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:58:57+00:00 2026-05-27T19:58:57+00:00

Need to parse a Class declaration line in Java using regular expression e.g. String

  • 0

Need to parse a Class declaration line in Java using regular expression e.g.

String line = "public class ActionDiagramReader extends XReader implements ActionHandler, Action {";
String line  = "public class ActionDiagramReader extends XReader{";

I am trying to use this regex

String regExp = ".*class\\s+(\\w+)(\\s+extends\\s+(\\w+))?(\\s+implements\\s+(\\w|,)+)?\\{.*$";
Pattern pattern = Pattern.compile(regExp, Pattern.DOTALL | Pattern.CASE_INSENSITIVE | Pattern.MULTILINE);
Matcher match = pattern.matcher(line1);

But this regex fails to parse classname/extender classname/implementer clas-name. Need to just identify all the classes & interfaces associated with given lines.

What is incorrect in my regular expression.
Thanks in advance

  • 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-27T19:58:58+00:00Added an answer on May 27, 2026 at 7:58 pm

    The last part in your regex isn’t grabbing the action.

    (\\s+implements\\s+(\\w|,)+)?\\{.*$
    

    It grabs up to ActionHandler, then dies because it’s looking for curly brace next.
    It my example below, I put the final \w into square braces and added \s to also capture any spaces, this way it will capture the “Action” at the end of the line as well.

    (\\s+implements\\s+([\\w,\\s]+))?\\s*\\{.*$
    

    See if that works for you.

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

Sidebar

Related Questions

Using Nokogiri, I need to parse a block given: <div class=some_class> 12 AB /
I need to parse document using SAX parser in java. I was able to
I need to parse a string like February 12, 1981 as a Date. I
I need to parse DTDs using PHP and am hoping there's a simple library
I need to parse an xml string and find values of specific text nodes,
I need to parse XML string with Linq, and I came up with the
I need to parse the java-objects which are passed through xml. I want to
I have this html code that I need to parse <a class=sushi-restaurant href=/greatSushi>Best Sushi
I'm writing routing class and need help. I need to parse $controller variable and
I need to parse a URL string like this one: &ad_eurl=http://www.youtube.com/video/4bL4FI1Gz6s&hl=it_IT&iv_logging_level=3&ad_flags=0&endscreen_module=http://s.ytimg.com/yt/swfbin/endscreen-vfl6o3XZn.swf&cid=241&cust_gender=1&avg_rating=4.82280613104 I need to

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.