Re: Search for multiple things in a string
- From: "tshad" <tscheiderich@xxxxxxxxxxxxxxx>
- Date: Mon, 19 Sep 2005 16:11:36 -0700
"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
news:MPG.1d99259a591cb91698c770@xxxxxxxxxxxxxxxxxxxxxxx
> tshad <tscheiderich@xxxxxxxxxxxxxxx> wrote:
>> I also feel that Regular Expressions, being an object in asp.net (not
>> necessarily C#) makes it just as valid as C#.
>
> Regular expressions have nothing to do with ASP.NET - they're a part of
> "normal" .NET.
Actually, you're right.
But that was my point.
Regex is part of .net as is C# (although it doesn't have to be) or VB.Net.
So using Regex is not really like using another language (as C# is different
from VB.Net).
But the discussion was valid in you use the best tool for the situation.
>
>> As far as readability, it has nothing to do with Regular Expressions
>> whether
>> it is readable or not, as Oliver mentions, but how you write it.
>
> No - I believe that searching for "jon.skeet" with IndexOf is clearer
> than searching for "jon\\.skeet" or @"jon\.skeet".
That's maybe true. But it would be clear to someone used to using both C#
and Regex.
Also, you have the same problem when dealing with web pages or getting a
file from the disk. You still use the escape character there (and as you
say, is a little confusing) - but you still do it.
>Which of them
> contains just the information which is actually of concern, and which
> contains information which is only present due to the technology used
> to do the searching?
>
>> You can also make some pretty unreadable C# code as well.
>
> Sure, but that's no reason to use regular expressions just to make
> things worse.
I agree with you that readability is important.
It used to be that people didn't like C and C++ for exactly the same reason
you point out. The code was not as clear as COBOL or Basic and that was the
complaint back then. I happened to be a Fortran programmer at that time and
was not interested to moving to C for that reason (not that Fortran was
better - readability wise).
The problem with C back that was that even though much of the code was
really cryptic. But it didn't have to be, that was just how people coded
back then. Mainly, it was important to make the most efficient code
possible because of the limited computing power and efficient rarely equates
to readable. And I am not even talking about compiling and linking and all
the options and cryptic command lines.
>
>> Readability is a function of the programmer not the language (in most
>> cases).
>
> Yes, but it's the programmer's decision how to approach things -
> whether you do things the simple way or the complex way. You *could*
> implement the string search by manually iterating over all the
> characters in the string, perhaps even writing your own state machine
> to do it. The code could be pretty readable considering what it's doing
> - but it's *bound* to be more complex than using IndexOf.
I agree.
Just because you can - doesn't mean you should.
>
>> As was also mentioned you also need to know the language. For someone
>> not used to objects, abstract objects and interfaces are also hard to
>> read.
>
> Sure - but why introduce unnecessarily complexity? You're already
> writing C#, so you'd better know C# - but why add regular expressions
> into the mix when they're unnecessary?
But if you know both and as I (and you) mentioned regex is part of .net as
is C# - so it is already in the mix. But you're right, don't introduce any
more complexity that necessary. But if it's 6 of one ... it's really up to
the programmer. In the original case, that was what it was. You can't tell
me that you feel that the solution suggested for this case was even close to
being unreadable (if you are even a stones throw from understanding Regular
Expressions).
I personally feel that both solutions are equally usable and readable (in
this situation).
I have also seen times when I just couldn't find an easy solution in C# or
VB and it was fairly easy in Regex.
I myself would usually opt for the C# or VB solutions first, but would have
no problem using Regex. As a matter of fact, I use Regex to strip commas
and $ from my textbox fields before writing it to SQL as it was the best
solution I could find. Such as:
SalaryMax.Text =
String.Format("{0:c}",CalculateYearly(Regex.Replace(WagesMax.Text,"\$|\,","")))
At the time, I couldn't seem to find as simple a solution as this in VB.Net
so I use this (not saying there isn't one).
>
>> I like seeing different options and make a choice. Sometimes I may use
>> something like Regex just so I am used to using it, as long as the
>> problem
>> warrants it.
>
> And that's the point - I don't think this problem *does* warrant it.
I agree that is isn't necessary here, but I don't think it is warranted or
unwarranted here. I think it's just as readable either way.
>
>> You don't use it - you lose it.
>
> So do you add a database when you just need to do a hashtable lookup,
> just in case you forget SQL? Do you use reflection to get at the value
> of a property, just in case you forget how to use that? I hope not.
Of course not. But as was mentioned there are times where Regex may be a
good solution and if you can do it either way, why not.
>
> It's very important to use appropriate technology, rather than using it
> for the sake of it. (It's one thing to experiment with technology for
> the sake of it as a learning tool, but I wouldn't do it in production
> code.)
Right. But Regex is not inappropriate technology. As you said, trying to
loop through each character when there is an easier way is a bit much.
But Regex is valid and is an appropriate method for handling strings and if
you are as comfortable with one as the other than it isn't inappropriate.
It's all in how you use it. And I was not saying experiment with it. I was
saying using it for the sake of staying familier with it. I don't want to
need to use it and have to figure it out when I need to use it.
As you said. Use the appropriate tool. If the appropriate tool is Regex,
it is going to be d... inconvenient to need it and not know how to use it.
Now I am not saying go out and learn every tool out there. But if it is a
valid tool in your particular environment, and it is available - why would
you not avail yourself of it?
Tom
> --
> Jon Skeet - <skeet@xxxxxxxxx>
> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
.
- Follow-Ups:
- Re: Search for multiple things in a string
- From: Jon Skeet [C# MVP]
- Re: Search for multiple things in a string
- References:
- Search for multiple things in a string
- From: tshad
- Re: Search for multiple things in a string
- From: Nicholas Paldino [.NET/C# MVP]
- Re: Search for multiple things in a string
- From: tshad
- Re: Search for multiple things in a string
- From: Jon Skeet [C# MVP]
- Re: Search for multiple things in a string
- From: Oliver Sturm
- Re: Search for multiple things in a string
- From: Jon Skeet [C# MVP]
- Re: Search for multiple things in a string
- From: Oliver Sturm
- Re: Search for multiple things in a string
- From: Jon Skeet [C# MVP]
- Search for multiple things in a string
- Prev by Date: Re: right installer for .NET application
- Next by Date: logging onto another site on another server
- Previous by thread: Re: Search for multiple things in a string
- Next by thread: Re: Search for multiple things in a string
- Index(es):
Relevant Pages
|
Loading