正则表达式,用于捕获结构化字符串的一部分

时间:2019-05-22 23:21:17

标签: regex regex-lookarounds regex-group regex-greedy

我想从字符串中提取一个子字符串。

top - 16:04:32 up 69 days, 2 min, 0 users, load average: 0.39, 0.39, 0.41|Tasks: 64 total, 1 running, 63 sleeping, 0 stopped, 0 zombie|%Cpu(s): 2.3 us, 11.3 sy, 0.0 ni, 86.3 id, 0.1 wa, 0.0 hi, 0.1 si, 0.0 st|KiB Mem: 511268 total, 171828 used, 339440 free, 71632 buffers|KiB Swap: 0 total, 0 used, 0 free. 67608 cached Mem|

从上面的字符串中:我只想要:

|KiB Mem: 511268 total, 171828 used, 339440 free, 71632 buffers| 

模式相同,但是数字可能会更改。

KiB\sMem\是我一直在尝试的正则表达式。

2 个答案:

答案 0 :(得分:1)

在这里,我们想基于字符串和所需的输出使用左右边界,并收集我们喜欢的数据,也许是类似于以下的表达式:

(\|KiB.+?buffers\|)

Demo

测试

const regex = /(\|KiB.+?buffers\|)/gm;
const str = `top - 16:04:32 up 69 days, 2 min, 0 users, load average: 0.39, 0.39, 0.41|Tasks: 64 total, 1 running, 63 sleeping, 0 stopped, 0 zombie|%Cpu(s): 2.3 us, 11.3 sy, 0.0 ni, 86.3 id, 0.1 wa, 0.0 hi, 0.1 si, 0.0 st|KiB Mem: 511268 total, 171828 used, 339440 free, 71632 buffers|KiB Swap: 0 total, 0 used, 0 free. 67608 cached Mem|`;
let m;

while ((m = regex.exec(str)) !== null) {
    // This is necessary to avoid infinite loops with zero-width matches
    if (m.index === regex.lastIndex) {
        regex.lastIndex++;
    }
    
    // The result can be accessed through the `m`-variable.
    m.forEach((match, groupIndex) => {
        console.log(`Found match, group ${groupIndex}: ${match}`);
    });
}

答案 1 :(得分:1)

您可以尝试以下方法:

let str = "top - 16:04:32 up 69 days, 2 min, 0 users, load average: 0.39, 0.39, 0.41|Tasks: 64 total, 1 running, 63 sleeping, 0 stopped, 0 zombie|%Cpu(s): 2.3 us, 11.3 sy, 0.0 ni, 86.3 id, 0.1 wa, 0.0 hi, 0.1 si, 0.0 st|KiB Mem: 511268 total, 171828 used, 339440 free, 71632 buffers|KiB Swap: 0 total, 0 used, 0 free. 67608 cached Mem|";

[match] = str.match(/\|KiB Mem:.+?\|/);

console.log(match);

正则表达式说明:

  

\|KiB Mem:匹配必须以此(包括)开始

     

.+?多次匹配任何字符,直到找到下面的下一个模式

     

\|匹配文字|