regex -多行多内容

kcugc4gi  于 2023-08-08  发布在  其他
关注(0)|答案(3)|浏览(80)

我在找一种模式

"changes": [
      "5.12.0",
      "5.14.0"
    ],
...
    "changes": [
      "1",
      "5.0.0",
      "5.10.1"
    ],
...
    "changes": [
      "4.4",
      "5.0.0",
      "5.10.1"
    ],

字符串
我不是Maven,我试过40或50种不同的解决方案,这是我最后一次尝试:

/"changes": \[\s*("([0-9](.+))*"(,+))*\s*\],/


我试了这个,它很有效,泰。

"changes": \[\s*("([0-9](.+))*"(,+)\s )+("([0-9](.+))*"\s)\],

whhtz7ly

whhtz7ly1#

我会分两步来做:
1.搜索“changes”后面括号内的版本列表:
/"changes":\s*\[\s*([^\]]+)\s*\]/ghttps://regex101.com/r/XHaxJ0/4
1.对于每个匹配,您将获得捕获组1中的版本列表:

"4.4",
      "5.0.0",
      "5.10.1"

字符串
然后,您可以使用/[\d.]+/g提取每个版本:https://regex101.com/r/XHaxJ0/2
JavaScript代码:

const input = `    "changes": [
      "5.12.0",
      "5.14.0"
    ],
...
    "changes": [
      "1",
      "5.0.0",
      "5.10.1"
    ],
...
    "changes": [
      "4.4",
      "5.0.0",
      "5.10.1"
    ],`;

const regexChangesContent = /"changes":\s*\[\s*([^\]]+)\s*\]/g;
const regexVersion = /[\d.]+/g;

// To fill with the found versions in the changes arrays.
let versions = [];

let matchChangesContent,
    matchVersion;

while ((matchChangesContent = regexChangesContent.exec(input)) !== null) {
  while ((matchVersion = regexVersion.exec(matchChangesContent[1])) != null) {
    versions.push(matchVersion[0]);
  }
}

console.log(versions);

问题变更后编辑

如果你只想删除“changes”条目,我会这样做:

const input = `    "changes": [
      "5.12.0",
      "5.14.0"
    ],
    "date": "01.01.2023",
    "changes": [
      "1",
      "5.0.0",
      "5.10.1"
    ],
    "property": "value",
    "should_stay": true,
    "changes": [
      "4.4",
      "5.0.0",
      "5.10.1"
    ],`;

const regexChanges = /"changes":\s*\[\s*[^\]]+\s*\]\s*,/g;

console.log(input.replace(regexChanges, ''));

yhived7q

yhived7q2#

对不起,如果我不澄清,我有一个JSON与3000 k行,与一个大数组的对象,我想删除所有的变化版本的任何对象。
解决方案是:

"changes": \[\s*("([0-9](.+))*"(,+)\s*)+("([0-9](.+))*"\s*)\],

字符串
谢谢!我今天早上花了4个小时,问了几分钟后发现:(

ql3eal8s

ql3eal8s3#

从我上面的评论…

// const import fs from 'fs';

function deleteEveryChangesArrayRecursively(data) {
  if (Array.isArray(data)) {

    data.forEach(item => deleteEveryChangesArrayRecursively(item));

  } else if (data && typeof data === 'object') {
    Object
      .entries(data)
      .forEach(([key, value]) => {
        if (key === 'changes' && Array.isArray(value)) {

          Reflect.deleteProperty(data, key);
        } else {
          deleteEveryChangesArrayRecursively(value);
        }
      });
  }
  return data;
}

/*function getDataFromJsonFile(path) {
  try {
    const json = fs
      .readFileSync(path, { encoding: 'utf8' });

    return { success: true, data: JSON.parse(json) };
  } catch (exception) {
    return { success: false, exception };
  }
}
function writeDataAsJsonFile(path, data) {
  try {
    fs.writeFileSync(path, JSON.stringify(data));

    return { success: true };
  } catch (exception) {
    return { success: false, exception };
  }
}*/

// mocking both, read and parse from file and stringify and write as file.
const getDataFromJsonFile = () => ({ data: structuredClone(mockData) });
const writeDataAsJsonFile = () => ({ success: true });

let { data = null, success, exception }
  = getDataFromJsonFile('./data.json');

if (data !== null) {
  ({ success, exception } = writeDataAsJsonFile(
    './data.json',
    deleteEveryChangesArrayRecursively(data),
  ));
}
console.log({ mockData, data, success, exception });
.as-console-wrapper { min-height: 100%!important; top: 0; }
<script>
  // mocking the parsed json.
  const mockData = {
    foo: [{
      changes: [0, 1, 2],
    }, {
      foobar: ['FOO', 'BAR'],
      changes: 'not an array',
    }, {
      foobaz: 'FOO BAZ',
      changes: [3, 4, 5],
    }, {
      foobiz: [{
        foobizbar: ['FOO', 'BIZ', 'BAR'],
        changes: 'not an array',
      }, {
        foobizbaz: 'FOO BIZ BAZ',
        changes: [6, 7, 8],
      }, {
        foobizbiz: 'FOO BIZ BIZ',
        changes: [9, 0, 1],
      }],
    }, {
      changes: [2, 3, 4],
    }, {
      foobar2: ['FOO', 'BAR', '2'],
      changes: 'not an array',
    }, {
      foobaz2: 'FOO BAZ 2',
      changes: [5, 6, 7],
    }, {
      foobiz2: [{
        foobiz2bar: ['FOO', 'BIZ', '2', 'BAR'],
        changes: 'not an array',
      }, {
        foobiz2baz: 'FOO BIZ 2 BAZ',
        changes: [8, 9, 0],
      }, {
        foobiz2biz: 'FOO BIZ 2 BIZ',
        changes: [1, 2, 3],
      }],
    }],
  };
</script>

相关问题