Regex get all strings after first pipe

Viewed 71

I need to get strings between first pipe to third pipe.

Eg given

DOWN | Origin - test-pd-2 | Pool - test-pd | TCP timeout occurred

match:

Origin - test-pd-2 | Pool - test-pd

I tried this, but it didn't work:

.*\\|(.*)\|
3 Answers

Using a capture group and a negated character class, you might also use

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

Regex demo

^.+\|(.+\|{1}.+)\|.+$

regex101

Note that backslashes might need to be escaped depending on the language you are using.

After a pipe |, match non-pipes, pipe, non-pipes:

(?<=\| )[^|]+\|[^|]+[^| ]

See live demo.

The entire match is your target

Related