擅长:python、mysql、java
<p>纯粹使用regex是不可能的,但是你可以用一行代码来逃避)</p>
<pre><code>import re
str = """{
"data": {
# Some information about field 1
# on multiple lines
"field1": "XXXXXXXXXX",
# Some more info on a single line
"field2": "XXXXXXXXXXX"
# Some information about field 1
# on multiple lines
# Some information about field 1
# on multiple lines
"field3": "#this would be ignored"
}
}"""
rex = re.compile("(^(?!\s*#.*?[\r\n]+)(.*?)([\r\n]+|$)|[\r\n]*^\s*#\s*)+", re.MULTILINE)
print rex.sub("\n", str).strip().split('\n\n')
</code></pre>
<p>输出:</p>
^{pr2}$