Xiphias
Xiphias

Reputation: 4716

Regex Python filtering a list of concatenated strings

I am struggling with a regular expression. It is supposed to match only those strings that are a list of 3 words separated by |.

A word may contain any character (no newline etc., of course) but |

Examples:

word01|wörd|wä4rd # only this should be matched
word04|würd|wä4rd|of
word02|wörd|wä4rd|off|j
word01|wörd

I'd like to match those that have exactly 2 |. A simple count function could do it, but that is not available in my case. So I need a regular expression.

This obviously does not do the trick:

^[^\|]+\|[^\|]+\|+[^\|]$

What's the correct regular expression? What's wrong with my approach?

Upvotes: 0

Views: 62

Answers (3)

Achrome
Achrome

Reputation: 7821

You can simply do this

^([^|]+\|){2}[^|]+$

Upvotes: 1

sphere
sphere

Reputation: 1350

Correct would be: '^[^|]+\|[^|]+\|[^|]+$'

Upvotes: 2

Toto
Toto

Reputation: 91385

You have misplaced the last + sign, instead of:

^[^\|]+\|[^\|]+\|+[^\|]$

use

^[^\|]+\|[^\|]+\|[^\|]+$
//               ^____^

Upvotes: 2

Related Questions