簡體   English   中英

正則表達式:匹配方括號之間的換行符或空格

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

如何找到方括號內的所有換行符或空格?

我有以下字符串:

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

我想刪除換行符和空格,以便我得到:

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

我如何使用 RegEx(和 Python)來做到這一點?

就像是:

import re

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

利用

import re

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

Python代碼

結果

{
  "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