I have this text:

`iif(instr(|Wellington, New Zealand|,|,|)>0,|Wellington, New Zealand|,|Wellington, New Zealand| & |, | & |New Zealand|) & | to | & iif(instr(|Jeddah, Saudi Arabia|,|,|)>0,|Jeddah, Saudi Arabia|,|Jeddah, Saudi Arabia| & |, | & |Saudi Arabia|) & iif(|Jeddah, Saudi Arabia|=||,||,| via | & |Jeddah, Saudi Arabia|)`

I can regex the text (below) to get a collection of all of the elements between the `|` characters. I get 18 matches, with match #1 being `|,|`

```
MatchCollection fields = Regex.Matches(str, @"\|.*?\|");

```

I then want to replace each of the matches with a place holder like `~0~`, `~1~`, `~2~` etc up to `~17~` so I can run the rest of my code. I don't care if all of the common text is replaced with the same place holder which would leave gaps in the place holders if I use all 18. 

My problem is that I can't just do a straight replace as replacing element #1 (`|,|`) in this part of the string `|Jeddah, Saudi Arabia|,|,|` will replace the first instance it finds, where regex correctly recognizes `|Jeddah, Saudi Arabia|` as one match and `|,|` as another match.

The result I seek is this:

`iif(instr(~0~,~1~)>0,~0~,~0~ & ~2~ & ~3~) & ~4~ & iif(instr(~5~,~1~)>0,~5~,~5~ & ~2~ & ~6~) & iif(~5~=~7~,~7~,~8~ & ~5~)`

with the increasing numbers being in an array that I build once I know how many matches I have. I keep the original values and swap them back later on which is the easy part.