I am looking for a regex to do the following:
- Return the index of the first instance of a given character not inside double quotes (I can guarantee a matching closing double quote will always be present and the character to search for will never itself be a double quote)
- Allow starting from
int startIndex
position
Speed is one of my primary concerns here so the number of iterations should be as small as possible.
Examples (all examples set to look for !
, but this might not always be the case):
!something
- should return 0!
should also return 0something!
should return 9"something!"
should fail"some!thing"!
should return 12!"!"!
should return 0""!
should return 2""!""
should return 2!something
withstartIndex == 2
should fail!something!
withstartIndex == 2
should return 10 (despite starting at position 2, the index of the character on the given string is still 10)
Since this is for .NET the intention is to use Regex.Match().Index
(unless a better alternative is provided).
If you really need regex version, you could use a pattern as follows.
Example, for input
You can search Index as follows
Where GetIndex is defined as
Output
Hope that helps.