Regex with comma delimited string seperated by pipe sign in .NET

Viewed 88

I am trying to get a regex for the following format below. It requires a "Name", any character except empty whitespace or nothing. A pipe sign always, then a list of items comma seperated, which can be any characters. The comma cant have a space after it though (, ).

Name|Item1,Item2,Item3

Below is what I have so far and its matching basically every time.

(.*)|([.*]+(,[\S]+)*)+

Invalids

Name|
Name
   |Item1,Item2
|Item1,Item2
Name|Item1, Item2
|
1 Answers

You can use

^[^|\s]+\|[^,|\s]+(?:,[^,|\s]+)*$

See the regex demo

Details

  • ^ - start of string
  • [^|\s]+ - one or more chars other than | and whitespace
  • \| - a | char
  • [^,|\s]+ - one or more chars other than ,, | and whitespace
  • (?:,[^,|\s]+)* - zero or more repetitions of a comma and then one or more chars other than ,, | and whitespace
  • $ - end of string.
Related