gpt4 book ai didi

regex - Notepad++ 函数列表 : return class even if empty

转载 作者:行者123 更新时间:2023-12-04 15:43:44 29 4
gpt4 key购买 nike

我正在尝试定义一个自定义函数列表(%appdata%/Notepad++ 中的 functionList.xml),以便将其用作导航器,用于作为软件输入的基于文本的长文件。

我设法让它几乎完美地工作,但仍有一个问题困扰着我:即使 Class 中没有函数,我也想返回它 .

首先,这是我的文本文件的示例(你们中的一些人可能会认出用于 eQuest 能源建模软件的 .inp 文件,但这无关紧要):

$ *********************************************************
$ ** **
$ ** Electric & Fuel Meters **
$ ** **
$ *********************************************************

$ ---------------------------------------------------------
$ Electric Meters
$ ---------------------------------------------------------

"EM1" = ELEC-METER
TYPE = UTILITY
..


$ ---------------------------------------------------------
$ Fuel Meters
$ ---------------------------------------------------------



$ ---------------------------------------------------------
$ Master Meters
$ ---------------------------------------------------------

"MASTER-METERS 1" = MASTER-METERS
MSTR-ELEC-METER = "EM1"
MSTR-FUEL-METER = "FM1"
..

$ ---------------------------------------------------------
$ Something else
$ ---------------------------------------------------------


"XXX" = blabla
..

这是我拥有的当前解析器:
<parser 
id="equest_full"
displayName="eQuest Full Function List">

<!-- Find The entire Class using a positive lookahead -->
<classRange
mainExpr="\$.*?\r\n\r\n.*?(?=\$)"
displayMode="node" >

<!-- Here we return just the Class name -->
<className>
<!-- Find only the text, using a negative lookahead to exclude multiple white spaces -->
<nameExpr expr='\w(\w|-|/| (?! ))+'/>
</className>

<!-- Here we find the "function" - here it's something that starts a line with "XXXX" = "blabla" -->
<function
mainExpr='^\".*?\" = [^ ]+' >
<functionName>
<!-- We already have exactly what we want with the above -->
<funcNameExpr expr=".*"/>
</functionName>
</function>
</classRange>
<function
mainExpr="\$.*?\r\n\r\n">
<!-- A poor attempt to display classes even if empty... doesn't work properly -->
<functionName>
<!-- Find only the text, using a negative lookahead to exclude multiple white spaces -->
<nameExpr expr="\w(\w|-|/| (?! ))+"/>
</functionName>
</function>
</parser>

它将在函数列表 Pane 中显示以下内容:
Function List in its current state

我非常希望它也可以在列表中显示“燃油表”,即使它是空的。其背后的原因是我将把它用作导航页面,因此我需要 anchor 来清空部分,这样我就可以快速去那里添加东西。

在 Notepad++ 中可以吗?任何帮助将不胜感激。

我一直在想的事情:如果我可以在那里编写常规代码,在 ClassRange > Function 下,我会做一个 IF 语句。如果找到 "^\".*?\"= [^ ]+",则继续其余的解析。否则,返回类第一行的位置。我不知道如何使用 RegEx 或它是否可行。

更新:我想我找到了一种可行的方法,但我无法找出正确的正则表达式。
基本上,我认为我当前的解析器的问题是当类为空时它仍然作为一个类返回,因此以后不会被类范围之外的函数接收。
我需要找到一种方法来修改它:
<classRange
mainExpr="\$.*?\r\n\r\n.*?(?=\$)"
displayMode="node" >

它不需要选择“燃油表”类。现在它确实接受了这个:
$ ---------------------------------------------------------
$ Fuel Meters
$ ---------------------------------------------------------

直到下一个 $ 符号。

如何修改为上面的正则表达式以确保至少有一个字符不是\n\r 或空格?

最佳答案

这个你搞清楚了吗?我不完全确定你在问什么,但使用这样的东西:

\$\s-+\s+\$(?:\s+\w+)+

你得到匹配的代码:
$ ---------------------------------------------------------
$ ANY ALPHANUMERIC SENTENCES HERE

但你到底想捕捉什么?

或者,如果你只想要类名,你可以这样做:
(?<=\$\s-+\s+\$\s+)\w+(?:\s+\w+)+

关于regex - Notepad++ 函数列表 : return class even if empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27316919/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com