簡體   English   中英

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

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

我正在Visual Basic中開發一個非常簡單的Java編譯器。 我想用Java代碼解析類名,然后將其粘貼到VB程序的文本框中。 例如:

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

我為此使用了Regex.Match ,但失敗了。 下面是我嘗試的代碼:

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

編輯:

有時源代碼如下所示:

class Football extends Sports{

在這種情況下,我想參加Football

有時是:

class Dog implements ISpeak{

在這種情況下,我想得到Dog

有時,類也都實現擴展 ,如下所示:

class hello implements Serializable extends Object {

有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"

您可以使用以下正則表達式:

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

該類的值將在索引為1的組中捕獲。在C#中,它將為: match.Groups[1].Value

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM