我有一个文本文件要处理,示例内容如下:
[FCT-FCTVALUEXXXX-IA] Name=value Label = value Zero or more lines of text Abbr= Zero or more lines of text Field A=1 Field B=0 Zero or more lines of text Hidden=N [Text-FCT-FCTVALUEXXXX-IA-Note] One or more note lines [FCT-FCT-FCTVALUEZ-IE-DETAIL] Zero or more lines of text [FCT-FCT-FCTVALUEQ-IA-DETAIL] Zero or more lines of text [FCT-_FCTVALUEY-IA] Name=value Zero or more lines of text Label=value Zero or more lines of text Field A=1 Abbr=value Field A=1 Zero or more lines of text Hidden=N
我需要找到这样的部分:
[FCT-FCTVALUEXXXX-IA] Name=value Label = value Zero or more lines of text Abbr= Zero or more lines of text Field A=1 Field B=0 Zero or more lines of text Hidden=N
提取FCT-FCTVALUEXXXX-AA,名称,标签,缩写,字段A和B并隐藏,然后找到相应的部分(如果存在):
[Text-FCT-FCTVALUEXXXX-IA-Note] One or more note lines
end将注释行提取为单个字符串。
我不在乎这些部分
[FCT-FCT-FCTVALUEZ-IE-DETAIL] Zero or more lines of text
这三种类型的节都可以出现在文件中的任何位置,包括结尾处,并且节之间的位置没有可预测的关系。
不能保证Abbr字段A和B的顺序,但是它们总是出现在“名称”和“标签”之后以及“隐藏”之前。
到目前为止我所拥有的:
strParse = "(%[FCT%-.-%-)([IF])([EA])%]%c+Name=(.-)%c.-Label=(.-)%c(.-)Hidden=(%a)%c" --cant pull everything out at once because the order of some fields is not predictable
for id, rt, ft, name, label, detail, hidden in strFacts:gmatch(strParse) do
--extract details
abbr=detail:match("Abbr=(.-)%c") --may be blank
if abbr == nil then abbr = "" end
FieldA = detail:match("Field A=(%d)")
FieldB = detail:match("Field B=(%d)")
--need to sanitise id which could have a bunch of extraneous material tacked on the front and use it to get the Note
ident=id:match(".*(%[FCT%-.-%-$)")..rt..ft
Note = ParseAutonote(ident) --this is a function to parse the note which I've yet to test so a dummy function returns ""
tblResults[name]={ident, rt, ft, name, label, abbr, FieldA, FieldB, hidden, note}
end
大多数方法都可以正常工作(经过数小时的工作),但是不起作用的部分是:
(".*(%[FCT%-.-%-$)")
应该可以排除最终出现的FCT-sometext- 在字符串ID
中我的逻辑:将搜索锚定到字符串的末尾,并捕获以“ [FCT-”开头并在字符串末尾以“-”结尾的最短字符串。
给出“ [FCT-_ABCD-PDQR-”或 “ [FCT-XYZ-DETAIL]文本行[FCT-_ABCD-PDQR-”它在我希望返回“ FCT-_ABCD-PDQR-”时返回nil。 (请注意,ABCD,PDQR等可以是包含Alpha,-和_的任意长度的文本。)
答案 0 :(得分:1)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" rel="stylesheet"/>
<table class="table">
<thead>
<tr>
<th class="col-xl">Links available:</th>
<th class="col-xl-auto">Category 1</th>
<th class="col-xl-auto">Category 2</th>
<th class="col-xl-auto">Category 3</th>
</tr>
</thead>
<tbody>
<tr>
<td class="col-xl"></td>
<td class="col-xl-3">
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
</td>
<td class="col-xl-3">
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
</td>
<td class="col-xl-3">
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
</td>
</tr>
<tr>
<td class="col-xl"></td>
<td class="col-xl-3">
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
</td>
<td class="col-xl-3">
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
</td>
<td class="col-xl-3">
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
</td>
</tr>
<tr>
<td class="col-xl"></td>
<td class="col-xl-3">
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
</td>
<td class="col-xl-3">
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
</td>
<td class="col-xl-3">
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
<button type="button" class="btn btn-outline-primary btn-sm">Small button</button>
</td>
</tr>
</tbody>
</table>
可以让您发现自己想要的方式,
(".*(%[FCT%-.-%-)$")
没有。 (".*(%[FCT%-.-%-$)")
和$
是锚点,必须位于模式的末尾或开头,它们不能出现在捕获闭包中。
当锚点字符出现在模式中的其他位置时,它们将成为您要查找的字符串的一部分,但不包括在集合中使用^
来排除字符的情况,即:排除大写字符{{ 1}}
以下是使用示例字符串和问题中的模式进行模式匹配的示例。
^