gpt4 book ai didi

ruby - PBXProject 文件的正则表达式

转载 作者:太空宇宙 更新时间:2023-11-03 16:33:10 25 4
gpt4 key购买 nike

致力于 XCode 项目文件解析器的纯 ruby​​ 实现,PBXProject ,并且在正则表达式方面几乎不需要帮助。

所以 PBXProject 文件有一堆奇怪的男女混合行,混合了内容。我现在拥有的是正则表达式, (.*?) = (.*?)(\/\* (.*)\*\/)?; ? 适用于更简单的情况(第一行)。但是对于第二行,它切得太早了(到第一个 ; -字符)。

isa = PBXBuildFile; fileRef = C0480C2015F4F91F00E0A2F4 /* zip.c */;

isa = PBXBuildFile; fileRef = C0480C2315F4F91F00E0A2F4 /* ZipArchive.mm */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; };

所以我想从这些行中得到的是简单的 name = value 对,即

isa = PBXBuildFile
settings = {COMPILER_FLAGS = "-fno-objc-arc"; }

用一个正则表达式实现这个的简单方法?

最佳答案

这个正则表达式可以正常工作:

[a-zA-Z0-9]*\s*?=\s*?.*?(?:{[^}]*}|(?=;))

注意只允许有一层括号,正则表达式不会处理嵌套的括号。

从您的示例中,将捕获以下行:

isa = PBXBuildFile
fileRef = C0480C2015F4F91F00E0A2F4 /* zip.c */
isa = PBXBuildFile
fileRef = C0480C2315F4F91F00E0A2F4 /* ZipArchive.mm */
settings = {COMPILER_FLAGS = "-fno-objc-arc"; }

下面是正则表达式的解释:

[a-zA-Z0-9]*\s*?=\s*?.*?(?:{[^}]*}|(?=;))

Options: ^ and $ match at line breaks

Match a single character present in the list below «[a-zA-Z0-9]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
A character in the range between “a” and “z” «a-z»
A character in the range between “A” and “Z” «A-Z»
A character in the range between “0” and “9” «0-9»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the character “=” literally «=»
Match a single character that is a “whitespace character” (spaces, tabs, and line breaks) «\s*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match any single character that is not a line break character «.*?»
Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»
Match the regular expression below «(?:(?={){[^}]*}|(?=;))»
Match either the regular expression below (attempting the next alternative only if this one fails) «(?={){[^}]*}»
Match the character “{” literally «{»
Match any character that is NOT a “}” «[^}]*»
Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
Match the character “}” literally «}»
Or match regular expression number 2 below (the entire group fails if this one fails to match) «(?=;)»
Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=;)»
Match the character “;” literally «;»

关于ruby - PBXProject 文件的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12314482/

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