获取C#文件中的字段和属性,使用正则表达式。

huangapple go评论76阅读模式
英文:

Get C# file fields and properties using regular expression

问题

我想要使用正则表达式匹配(从.cs文件中选择)属性名称、字段名称以及它们被使用的位置。

这是一个示例类:

using System;
... // 使用的命名空间
using Grpc.Net.Client;
using UI = Gtk.Builder.ObjectAttribute;

namespace DummyProject;

public class MainWindow : Window
{
    [UI] private readonly MenuItem _aboutMenuItem;
    [UI] private readonly MenuItem _openFolderItem;
    [UI] private readonly MenuItem _openFileItem;
    [UI] private readonly Viewport _fileViewport;
    [UI] private readonly Notebook _codeNotebook;
    private readonly SourceView _codeSourceView;
    private readonly FileViewer _fileViewer;
    private readonly Files.FilesClient _filesClient;
    
    public FileSystemViewer FileSystemViewer { get; }
    
    public MainWindow() 
    {
        _fileViewer = new FileViewer();
        // 一些代码
    }

    // 方法

结果将是:_aboutMenuItem, _openFolderItem, _openFileItem, _fileViewport, _codeNotebook, _codeSourceView, _fileViewer, _filesClient, FileSystemViewer

我尝试了以下正则表达式:^(?:(?=[^()]*$))?(?:(?!.*\b(namespace|return|using|class)\b))[\t \b]*(?:[\[\]a-zA-Z0-9_.]+\s+)?(?:(?:private|public|protected|internal)\s+)?(?:(?:static readonly|static|const|readonly|volatile|virtual|override)\s+)?(?:(?:[a-zA-Z0-9\.]+)\s+)?(\w+)\s*

但是这个正则表达式匹配错误(请参见https://regex101.com/r/ZbctGa/3)

有什么建议吗?

英文:

I want to match (select from .cs file) property names, field names and places where they are used using regular expression.

This is example class:

using System;
... // usings
using Grpc.Net.Client;
using UI = Gtk.Builder.ObjectAttribute;

namespace DummyProject;

public class MainWindow : Window
{
    [UI] private readonly MenuItem _aboutMenuItem;
    [UI] private readonly MenuItem _openFolderItem;
    [UI] private readonly MenuItem _openFileItem;
    [UI] private readonly Viewport _fileViewport;
    [UI] private readonly Notebook _codeNotebook;
    private readonly SourceView _codeSourceView;
    private readonly FileViewer _fileViewer;
    private readonly Files.FilesClient _filesClient;
    
    public FileSystemViewer FileSystemViewer { get; }
    
    public MainWindow() 
    {
        _fileViewer = new FileViewer();
        // some code
    }

    // methods

Result will be: _aboutMenuItem, _openFolderItem, _openFileItem, _fileViewport, _codeNotebook, _codeSourceView, _fileViewer, _filesClient, FileSystemViewer

I've tried this regular expression: ^(?:(?=[^()]*$))?(?:(?!.*\b(namespace|return|using|class)\b))[\t \b]*(?:[\[\]a-zA-Z0-9_.]+\s+)?(?:(?:private|public|protected|internal)\s+)?(?:(?:static readonly|static|const|readonly|volatile|virtual|override)\s+)?(?:(?:[a-zA-Z0-9\.]+)\s+)?(\w+)\s*

But this regex matches incorrectly (see https://regex101.com/r/ZbctGa/3)

Any suggestions?

答案1

得分: 1

以下是翻译好的部分:

_aboutMenuItem
_openFolderItem
_openFileItem
_fileViewport
_codeNotebook
_codeSourceView
_fileViewer
_filesClient
FileSystemViewer
英文:

You can use the following to capture the properties and fields.

(?:public|private)(?! class) (?:readonly )?(?:.+?) (.+?)[ ;]

Output

_aboutMenuItem
_openFolderItem
_openFileItem
_fileViewport
_codeNotebook
_codeSourceView
_fileViewer
_filesClient
FileSystemViewer

huangapple
  • 本文由 发表于 2023年6月12日 00:38:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定