andrewg
andrewg

Reputation: 175

Regex to extract the main class in java source code

I have a multi-class java file. One of those classes contains a main method (public static void main...). Example:

class one {...}
class two {...}
class three {
   public static void main(String[] args) {...}
}

I tried

class.*?[^.*?]*?main

but that matches starting at class one not class three

Upvotes: 1

Views: 288

Answers (1)

SADIK Younsse
SADIK Younsse

Reputation: 166

\s*static\s*void\s*main\s*\(\s*String\s*\[\]\s*[^\)]*\)

if you want the name of class you can test this regex

class\s*([^\{]*)\{\s*.*static\s*void\s*main\s*\(\s*String\s*\[\]\s*[^\)]*\).*

Upvotes: 2

Related Questions