Getting substrings from a string c# I have a c# application which displays the names of assessments and the zone which they are performed upon, separated by a comma and a space. For instance:


assessment1, Firewall
mom's assessment, LAN


When the user selects on of these assessments, I need to separate the assessment name from the zone name. I also need to replace any single quotes in the string by two single quotes so I can query my database without issues (this i cannot change, it has to be done because no SQLiteParameters are used in the function that queries my database).

This is how I do this currently (note that _view.SelectedAssessment is the entire string of both the assessment name and zone): 

string zone = _view.SelectedAssessment.Substring(_view.SelectedAssessment.IndexOf(", ") + 2);
string assessmentName = _view.SelectedAssessment.Substring(0, _view.SelectedAssessment.Length - zone.Length - 2);


Is there a better way to do this? Any improvements?
Make it regex with named groups:

var matcher = new Regex(@"(?&lt;assesment&gt;.+?),\s*(?&lt;zone&gt;.+)");
var match = matcher.Match("assessment1, Firewall");
var assesment = match.Groups["assesment"].Value;
var zone = match.Groups["zone"].Value;


where


(?&lt;assesment&gt;.+?) - catches assesment ungreedy ? so that it stops at ,
,\s* - the separator is , with an optional white space (zero or more times)
(?&lt;zone&gt;.+) - catches zone greedy


Sample at regex101.com



I wasn't aware of that but @Vogel612 kindly mentioned that backtracking as I use it in my solution above might be sometimes a problem: Why Using the Greedy .* in Regular Expressions Is Almost Never What You Actually Want and The Explosive Quantifier Trap.

Using the (?&lt;assessment&gt;[^,]+) expression would be much faster and safer here.


  That change drops the match you propose from 30 steps until matching to 9, so it saves roughly 60% of execution time.




Another valuable remark by @forsvarir suggests using the statick Regex.IsMatch method over creating a new instance each time. This should be much faster here.


  The difference in performance is due to the caching of regular expressions used in static method calls.


There is a lot more to that with pleny of examples on Optimizing Regular Expression Performance, Part I: Working with the Regex Class and Regex Objects [Ron Petrusha]



Why do I prefer the regex solution?


I can test it with regex101
I can use the same pattern for validation and data extraction. I know whether the data is in the correct format without checking each element of the array values[i] separately.
I can easier restrict the characters or casing or splitting rules
I can use named groups which I find easier to understand because I can see what I work with rather then just having a string[] especially that each filed has a meaning - otherwise I'd use the Regex.Split method if was just an array of some values
I can trim the data at the same time (requires some additional \s*)
I can use the same pattern for Regex.Replace if I wanted to change it