英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论