简体   繁体   English

C#正则表达式仅在没有评论的情况下匹配

[英]C# Regex match only if not comment

I am writing an application for Avisynth-editing and try to get all variables which are defined as sources like the following ones with Regex: 我正在编写一个用于Avisynth编辑的应用程序,并尝试获取所有变量,这些变量被定义为正则表达式中的以下源:

Back = ImageReader("..\Backgrounds\Background_04.jpg", 0, Video.Framecount, 30, true).Spline16Resize(1920, 1080)

Video = ImageReader(".\Bilder\NDS Aufnahmebild.png", 0, 1000, 30, true)

I am using the following Regex which works great so far (global, case insensitive): 我正在使用以下正则表达式,到目前为止效果很好(全局,不区分大小写):

(?<var>[^#\s\t]\w+)(?:\s|\t)+?=(?:\s|\t)+?(ImageSource|ImageSourceAnim|ImageReader|ImageWriter)\((.*?)\)(?:\.|\n)?

The output would be: 输出为:

Group "var" : Video 组“ var”:视频

Group 0: ImageReader 组0:ImageReader

Group 1: ".\\Bilder\\NDS Aufnahmebild.png", 0, 1000, 30, true 组1:“。\\ Bilder \\ NDS Aufnahmebild.png”,0、1000、30,是

However it won't ignore comments which is really embarrasing. 但是,它不会忽略确实令人尴尬的评论。 I want to capture the string only if there is no # before the match. 我只想在比赛前没有#的情况下捕获字符串。 I don't think that it's that complicated but at the moment I just don't get it. 我不认为这很复杂,但目前我还不了解。

I hope anybody can help me. 我希望有人能帮助我。 I am using .Net Framework 4 and don't want to change the version. 我正在使用.Net Framework 4,但不想更改版本。

As long as it is only # at beginning of the line, then you can simply try-match the # and then check if it actually did match it. 只要在行首只有# ,那么您就可以简单地尝试匹配# ,然后检查它是否确实匹配它。 If it didn't it is not commented, otherwise it is. 如果没有,则不评论,否则。

(?:\s|\t)*?(?<comment>#)?(?:\s|\t)*?(?<var>[^#\s\t]\w+)(?:\s|\t)+?=(?:\s|\t)+?(ImageSource|ImageSourceAnim|ImageReader|ImageWriter)\((.*?)\)(?:\.|\n)?

If comment group contains a # , then the line is commented out, so just ignore it in your application. 如果comment组包含# ,那么该行将被注释掉,因此在您的应用程序中将其忽略即可。

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

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