Home > Software engineering >  Remove the first two columns from a csv file with REGEX
Remove the first two columns from a csv file with REGEX

Time:10-05

I am having trouble removing the first two columns from a CSV file using REGEX. Usually I would have used the expression "^(?:[^,] ,){2}", but the structure of my file prevents me from doing that (there may be null columns).

Here is an example of my data: https://regex101.com/r/Rzi8de/1

The desired result is:

value3,value4
value3,value4
value3,value4
,value4

I thank you in advance!

CodePudding user response:

I think the main problem with your snippet is using (1 or more repetitions) instead of * (zero or more repetitions). What about something like this?

^([^,]*,){2}

"At the beginning of a string, match two occurrences of anything-except-comma (including nothing, as denoted by *) followed by a comma"

  • Related