Don't name a variable str
str
is a builtin, which you'll no longer be able to use if you mask it with a variable named str
.
Issues with current regex
You asked why you are not getting proper results. One reason is that your regex is greedy. The (.*)
will match |
too.
A second challenge is that Python does not support repeated capturing groups.
A repeated capturing group will only capture the last iteration. Put a capturing group around the repeated group to capture all iterations or use a non-capturing group instead if you're not interested in the data (regex101.com).
An easier way is to split based on the delimiter.
With str.split()
>>> s = "| id_number | Category | Description |"
>>> s.strip("| ").split(" | ")
['id_number', 'Category', 'Description']
With re.split()
The str.split()
solution above assumes the exact delimiter |
. Alternatively you could use:
>>> re.split(r"\s+\|\s+", s.strip("| "))
['id_number', 'Category', 'Description']
to account for extra whitespace.