Given text such as:
This is my [position].
Here are some items:
[items]
[item]
Position within the item: [position]
[/item]
[/items]
Once again, my [position].
I need to match the first and last [position], but not the [position] within [items]...[/items]. Is this doable with a regular expression? So far, all I have is:
Regex.Replace(input, @"\[position\]", "replacement value")
But that is replacing more than I want.
As Wug mentioned, regular expressions aren’t great at counting. An easier option would be to just find the locations of all of the tokens you’re looking for, and then iterate over them and construct your output accordingly. Perhaps something like this:
(Disclaimer: Have not tested. Or even attempted to compile. Apologies in advance for inevitable bugs.)