简体   繁体   English

正则表达式:匹配方括号之间的换行符或空格

[英]RegEx: match line breaks or spaces between square brackets

How can I find all line breaks or spaces that are inside square brackets?如何找到方括号内的所有换行符或空格?

I have the following string:我有以下字符串:

{
  "decay_id": {
    "int_only": true,
    "feature_type": "categorical",
    "category_values": [
      0,
      1
    ],
    "category_names": [
      "d1",
      "d2"
    ]
  }
}

And I want to delete line breaks and spaces so that I get:我想删除换行符和空格,以便我得到:

{
  "decay_id": {
    "int_only": true,
    "feature_type": "categorical",
    "category_values": [0,1],
    "category_names": ["d1","d2"]
  }
}

How can I use RegEx (and Python) to do this?我如何使用 RegEx(和 Python)来做到这一点?

Something like:就像是:

import re

output = re.sub("some RegEx", "", input)

Use利用

import re

output = re.sub(r"\[[^][]*]", lambda z: re.sub(r'\s+', '', z.group()), input)

See Python codePython代码

Results :结果

{
  "decay_id": {
    "int_only": true,
    "feature_type": "categorical",
    "category_values": [0,1],
    "category_names": ["d1","d2"]
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM