简体   繁体   English

在vb.net中使用regex.match查找类名

[英]find class name using regex.match in vb.net

I'm devaloping a very simple Java compiler in Visual Basic. 我正在Visual Basic中开发一个非常简单的Java编译器。 I want to parse the class name in a Java code which I paste into a textbox of my VB program. 我想用Java代码解析类名,然后将其粘贴到VB程序的文本框中。 For example: 例如:

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

I used Regex.Match for this but I failed. 我为此使用了Regex.Match ,但失败了。 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 . 在这种情况下,我想参加Football

And sometimes it is: 有时是:

class Dog implements ISpeak{

In this case I want to get Dog . 在这种情况下,我想得到Dog

Sometimes the classes both also implement and extend , like this: 有时,类也都实现扩展 ,如下所示:

class hello implements Serializable extends Object {

There are 4 patterns: 有4种模式:

  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 该类的值将在索引为1的组中捕获。在C#中,它将为: match.Groups[1].Value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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