Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there particular cases where native text manipulation is more desirable than regex?

Tags:

string

regex

Are there particular cases where native text manipulation is more desirable than regex? In particular .net?

Note: Regex appears to be a highly emotive subject, so I am wary of asking such a question. This question is not inviting personal/profession opinions on regex, only specific situations where a solution including its use is not as good as language native commands (including those which have underlying code using regex) and why.

Also, note that Desirable can mean performance, can mean code-readability; it does not mean panacea, as each solution for a problem has its benefits and limitations.

Apologies if this is a duplicate, I have searched SO for a similar question.

like image 894
StuperUser Avatar asked Jan 21 '26 04:01

StuperUser


1 Answers

I prefer text manipulation over regular expressions to parse delimited string input. It's far simpler (for me at least) to issue a string split than to manage a regular expression.

Given some text:

value1, value2, value3

You can parse the line easily:

var values = myString.Split(',');

I'm sure there's a better way but with regular expressions you'd have to do something like:

var match = Regex.Match(myString, "^([^,]*),([^,]*),([^,]*)$");
var value1 = match.Group[1];
...
like image 112
Ray Vernagus Avatar answered Jan 23 '26 21:01

Ray Vernagus