Search code examples
regexcsvregex-greedy

Regex to split and extract value from pipe delimited file


I want to split a pipe( | ) delimited file using regex only and extract a particular field at nth position. My current solutions works fine until a blank field appears. I am unable to figure out what i am doing wrong.

Sample Data:

asdw|qwe|23344|as||sada||ssss|sdd

My partially working solution:

^((?:[^|]+\|){3})(?P<error>[^|]+)

https://regex101.com/r/bXvo4T/1

The issue with the current solution is it fails when two consecutive delimiter appears without any content in between, as for position 4 it should return no match but ends up giving no match for all occurences after 4th position. You can try that in the regex101 link shared.


Solution

  • Just need to swap the 'One or more token +' with a 'Zero or more token *' as it looks like it's getting stuck with no chars between the two pipes.

    I think the following should give you the result you are looking for:

    ^((?:[^|]*\|){3})(?P<error>[^|]+)