Splitting a String

  • Thread starter Thread starter Materialised
  • Start date Start date
Basically, using regular expressions means you need to understand two
languages instead of one. That's fine when there's a significant
*benefit* in using regular expressions - but in this case there isn't.

Yes, you're right. I was speaking in a general sense about the cryptic
appearance of regular expressions. Using them when appropriate is important.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

The man who questions opinions is wise.
The man who quarrels with facts is a fool.
 
Kevin Spencer said:
Yes, you're right. I was speaking in a general sense about the cryptic
appearance of regular expressions. Using them when appropriate is important.

I think that's important. Regular expressions are basically
write-only, like Perl. If someone wants to modify your code they'll
have to go through mental effort that's equivalent to rewriting the
regex from scratch.
 
Mark Wilden said:
But in other situations there will be a benefit to using regex's. Which
means that it behooves any programmer to understand them.

That could be said of any number of technologies. I don't know all
technologies available in .NET, even though I dare say there are very
few which have no benefit in *any* situation. I don't know XQuery yet -
I will only learn it when I come across a situation where there's
sufficient benefit in it to make it worth effectively making it a pre-
requisite for understanding the code.
Which means that there should be no disadvantage to using them.

There is when the regex solution is harder to read/understand than the
alternative, which is the case here, I believe. Even if one understands
regexes well, it's fairly easy to make mistakes in them.

There are also performance disadvantages too in this case. In fact, I
see no advantages in using regular expressions here at all - it seems
to me that it's just another case where some people view every problem
involving strings as a place to use regular expressions :(
 
Jon Skeet said:
That could be said of any number of technologies. I don't know all
technologies available in .NET, even though I dare say there are very
few which have no benefit in *any* situation. I don't know XQuery yet -
I will only learn it when I come across a situation where there's
sufficient benefit in it to make it worth effectively making it a pre-
requisite for understanding the code.

Regular expressions are far more general purpose than XQuery and have been
around far longer than .NET. If folks don't care to add regex to their
toolboxes, that's fine, I suppose, but I think they're missing out.
 
Mark Wilden said:
Regular expressions are far more general purpose than XQuery and have been
around far longer than .NET. If folks don't care to add regex to their
toolboxes, that's fine, I suppose, but I think they're missing out.

I agree that it's a good thing to know. I disagree that it's worth
everyone becoming so good at them that they can read code using regular
expressions like the ones in these thread (which aren't *that*
complicated, even) as easily as the version that Nicholas posted.

Unless it's easier to read than the simpler version using
String.Substring, your claim that there should be no disadvantage to
using the regex version doesn't hold water.
 
Unless it's easier to read than the simpler version using
String.Substring, your claim that there should be no disadvantage to
using the regex version doesn't hold water.

Actually, I do think that regular expressions are (usually) easier to read
than the equivalent procedural code. But I'm willing to admit that I might
be a freak in this regard. :)
 
Mark Wilden said:
Actually, I do think that regular expressions are (usually) easier to read
than the equivalent procedural code. But I'm willing to admit that I might
be a freak in this regard. :)

It depends where you "usually" see them. If you "usually" only use them
appropriately to start with, that's entirely reasonable :) In this
case, I think the Substring version is much clearer - and there have
been other similar examples, including checking whether one string
starts with another or not!

I agree that if the logic is very involved, a regular expression *can*
be an awful lot simpler - but I don't think it should always be the
first line of attack whenever you come across something that requires
string manipulation/validation.
 
there have
been other similar examples, including checking whether one string
starts with another or not!

In that particular case, I do agree that String.StartsWith is clearer.
 
Back
Top