这个正则表达式可以正常工作:
[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 «;»