简体   繁体   中英

find class name using regex.match in vb.net

I'm devaloping a very simple Java compiler in Visual Basic. I want to parse the class name in a Java code which I paste into a textbox of my VB program. For example:

class MyPro {   // in this case i need to get "MyPro"

I used Regex.Match for this but I failed. Below is the code I tried:

Dim regex As Regex = New Regex("(class)*{")
    Dim match As Match = regex.Match("class mypro{")
    If match.Success Then
        Console.WriteLine(match.ToString)
    End If

Edit:

Sometimes the source code looks like:

class Football extends Sports{

In this case I want to get Football .

And sometimes it is:

class Dog implements ISpeak{

In this case I want to get Dog .

Sometimes the classes both also implement and extend , like this:

class hello implements Serializable extends Object {

There are 4 patterns:

  1. class MyPro { //I want to get "MyPro"
  2. class Football extends Sports{ //"Football"
  3. class Dog implements ISpeak{ //"Dog"
  4. class hello implements Serializable extends Object { //"hello"

You could use the following regular expression:

class\s+([^\s]+)[\s\r\n{]

The value of the class will be captured in the group with index = 1. In C#, it would be: match.Groups[1].Value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM