使用正则表达式在我选择的前两个相同字符之间查找文本

Find text between the first two same characters of my choice with regex

Assume the following output

.

20198818-119903 | firmware-check | passed: host test-1000 is connected to a test with Version: 333 | |
20198818-119903 | other-test-123 | passed: host test-1000 is connected to a test with 333:
20198818-119903 | test4| passed :| host | is connected to a test with 333
20198818-119903 | something | passed: host test-1000 is connected to a test with Version:

I want to extract firmware-check, other-test-123, test4, and something

所以基本上 前两个 垂直条之间的所有内容

我试图用 txt2re 解决这个问题,但没有达到我想要的效果(例如,不忽略第三行的主机)。我从来没有使用过正则表达式,也不想仅仅为了这个特殊情况而学习它。有人可以帮帮我吗?

此表达式将简单地提取这些值:

.*?\|\s*(.*?)\s*\|.*

DEMO

const regex = /.*?\|\s*(.*?)\s*\|.*/gm;
const str = `20198818-119903 | firmware-check | passed : host test-1000 is connected to a test with Version: 333 | |
20198818-119903 | other-test-123 | passed : host test-1000 is connected to a test with 333:
20198818-119903 | test4| passed :| host | is connected to a test with 333
20198818-119903 | something | passed : host test-1000 is connected to a test with Version:`;
const subst = ``;

// The substituted value will be contained in the result variable
const result = str.replace(regex, subst);

console.log(result);

你可以用这个

^[^|]+\|([^|]+)

let str = `20198818-119903 | firmware-check | passed: host test-1000 is connected to a test with Version: 333 | |
20198818-119903 | other-test-123 | passed: host test-1000 is connected to a test with 333:
20198818-119903 | test4| passed :| host | is connected to a test with 333
20198818-119903 | something | passed: host test-1000 is connected to a test with Version:`

let op = str.match(/^[^|]+\|([^|]+)/gm).map(m=> m.split('|')[1].trim())

console.log(op)

您可以使用正则表达式 /[^|]+\|\s*([^|]+)\s*\|.*/| 之间的第一个匹配项放入捕获组。使用 exec 和 while 循环获取所有匹配项。

(你也可以使用matchAll跳过while循环,但它仍然处于草稿状态)

let str = `20198818-119903 | firmware-check | passed : host test-1000 is connected to a test with Version: 333 | |
20198818-119903 | other-test-123 | passed : host test-1000 is connected to a test with 333:
20198818-119903 | test4| passed :| host | is connected to a test with 333
20198818-119903 | something | passed : host test-1000 is connected to a test with Version:`,
    regex = /[^|]+\|\s*([^|]+?)\s*\|.*/g,
    match,
    matches = [];

while(match = regex.exec(str))
  matches.push(match[1])

console.log(matches)

// OR
// matchAll is still in Draft status
console.log(
  Array.from(str.matchAll(regex)).map(a => a[1])
)

Regex demo