Wojciech Rogman
Wojciech Rogman

Reputation: 5

Regex match last match before group

I can't make a good regex to find last content between pipe characters | before specified match.

I want to find " 313.175,89-" (or whatever could be between | signs) immediately before |401 |.

|Posting text | Amount|BTC | | |Beginning balance | 0,00 | | | 2|29.07.2021|29.07.2021| 401 ARD DEP. PLN OPROC.: 0 OD: 29.07.2021 DO: | | 313.175,89-|401 | | | | | KAPITALU: 612313

All I have is \|.*?(?=\|401.\|) but matching everything from newline sing.

Upvotes: 0

Views: 63

Answers (1)

Bohemian
Bohemian

Reputation: 425033

Your regex is almost there, but the problem is your regex will match from the first pipe due to .*? also matching pipes.

Replace \|.*? with [^|]* to match only non-pipes:

[^|]*(?=\|401 \|)

See live demo.

This works by matching any number of characters that are not a pipes and that are followed by |401 |.

Note that the pipe within the character class does not have to be escaped, because most characters lose their special meaning when used in a character class and pipe is one of them.


To also omit the leading/trailing spaces of your target, use [^| ]*(?= *\|401 \|).

See live demo.

Upvotes: 1

Related Questions