lua - Lua 模式——我怎样才能让它工作?
问题描述
我有一个文本文件要处理,一些示例内容如下:
[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、Name、Label、Abbr、Field A和B以及Hidden,然后找到对应的section(如果存在的话):
[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%-.-%-$)")
这应该在字符串 id 中提取 FCT-sometext- 的最终出现
我的逻辑:将搜索锚定到字符串的末尾,并在字符串末尾捕获以“[FCT-”开头并以“-”结尾的最短字符串。
给定“[FCT-_ABCD-PDQR-”或“[FCT-XYZ-DETAIL]文本行[FCT-_ABCD-PDQR-”的值,当我希望它返回“FCT-_ABCD-PDQR-”时它返回nil ”。(注意 ABCD、PDQR 等可以是包含 Alpha、- 和 _ 的任意长度的文本)。
解决方案
当您发现自己(".*(%[FCT%-.-%-)$")
按照您想要的方式工作时,哪里(".*(%[FCT%-.-%-$)")
没有。$
并且^
是锚点,必须出现在模式的末尾或开头,它们不能出现在捕获闭包内。
当锚字符出现在模式中的其他任何位置时,它们将成为您要查找的字符串的一部分,不包括^
在集合中用于排除字符的情况,即:不包括大写字符[^A-Z]
以下是使用示例字符串和问题中的模式进行模式匹配的示例。
print(string.match("[FCT-_ABCD-PDQR-", (".*(%[FCT%-.-%-$)"))) -- initial pattern
> nil
print(string.match("[FCT-_ABCD-PDQR-$", (".*(%[FCT%-.-%-$)"))) -- $ added to end of string
> [FCT-_ABCD-PDQR-$
print(string.match("[FCT-_ABCD-PDQR-", (".*(%[FCT%-.-%-)$"))) -- $ moved to end of pattern
> [FCT-_ABCD-PDQR-
推荐阅读
- python - 在 Thread 构造函数中定义 `args` 的更好方法?
- android - 对包含字符串值的 Mediastore 轨道号进行排序时出现问题
- biztalk-2016 - BizTalk - 设置电子邮件 contentType 导致错误:XML 文档中存在错误 (1, 1)
- python-3.8 - 检查字符串索引时“==”与“in”或“if in”之间有什么实际区别吗?
- python - 将多个 yaml 文件读取到 pandas Dataframe
- javascript - 如何为 HTML 中动态添加的元素添加索引?
- asp.net-core - 带有 ViewModel 无参数构造函数的 ASP.NET Core 3.1 DI
- laravel - Sanctum 和 Postman 的 SPA 身份验证问题
- flyway - 航路迁移不工作需要维修
- hyperledger-fabric - 为什么在超级账本中部署链码不起作用?