Re: function to match parenthesis '(' and ')'

Tech-Archive recommends: Fix windows errors by optimizing your registry



Thanks all for all your comments about this debate. That 's very interesting
to see all these points of view.

I agree on all everybody have said since the beginning except for 2 things :

1) You say that in ten lines, you are able to write something that can say
if the balance matching is correct. You did not say how many lines it takes
to have a collection with all matching subelements. Again, in one line, that
is done. Do you know why there is XPath for xml ? To simplify treatment for
selecting a node. In one line, you can reach the nodes you wanted without
rewritting the same similar code may times. I consider it's the same with
Regex.
2) There some case about little changes where the regex modification is
quicker than a code like marcus' one. If instead of evaluating expression
with only parenthesis, you want to find all arguments of a function in a
dynamical compilation of a source code. You only have to add the name of the
function between the first parenthesis to match. With a code like marcus's
one, you will have to write a string parser wich is already implemented in
regex.

Seeing all you replies, I modify what I said at the beginning of this
debate. Maybe only for matching parenthesis, it's better not to use regex.
When you have to add parsing with the parenthesis matching, I'm sure that
regex is the proper tool.

P.S. Michael : I could not stop laughting seeing your reply about me. Why
not try to work in the same company ? It should be very interesting.... ;-)

Ludovic.


"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> a écrit dans le message de
news:MPG.1dc725f59f46dfba98c99d@xxxxxxxxxxxxxxxxxxxxxxx
> Ludovic SOEUR <Ludovic_SOEUR@xxxxxxxxxxx> wrote:
> > > I certainly appreciate that regexes are useful. I just don't think
this
> > > is a place where they're suitable. I would guess that at least half of
> > > the requests for regular expressions I see on the newsgroups are
> > > inappropriate places to apply regular expressions. A lot of people
seem
> > > to approach any textual problem with the immediate idea of "use a
> > > regular expression!" without considering whether or not it's the
> > > best/simplest way of solving it.
> >
> > Yes, I agree with you. that is a good summary of Regex usage today.
> > I will however add, for this topic (validating and parsing expressions
with
> > parenthesis), that a known regex does all the job requested.
> > Of course you can write code, write your own libraries that should be
more
> > elegant (it depends on what you think is elegant). I prefer reuse what
is
> > already done and only optimise it if necessary.
>
> You had to go to a web page to find that code. I'm sure you *could*
> have found something like Marcus's code on a web page. Alternatively,
> the code that Marcus wrote was easy to write in the first place - no
> need to hunt it down.
>
> Given the option of "look for something that someone else has done,
> which I (and anyone reading the code) will need to put significant
> effort into understanding" and "write something from scratch in about
> 10 lines of easily understandable code" I'll pick the second one every
> time.
>
> --
> 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


.



Relevant Pages

  • Re: regex, negations, grep, find and replace (a few questions)
    ... I do not know much regex. ... But it seems as if you define each character ... be aware that different tools may use slightly different syntaxes for the same regular expressions. ... expresion" might mean "matching everything not matching the regular expression" or, in other words, removing everything matching the regexp. ...
    (alt.os.linux)
  • regular expressions, substituting and adding in one step?
    ... Now, matching the middle part and replacing it with '%s' is easy, but how would I add the extra string to the end of the line? ... Is it done all at once, or must I make a new regex to match? ... So the questions are, how do you use regular expressions to add text to the end of a line, even if you aren't matching the end of the line in the first place? ...
    (comp.lang.python)
  • Re: Can a regex match numbers?
    ... > Is it possible to specify number matching in a regex, ... IMO, using regular expressions to check the value of ...
    (perl.beginners)
  • Re: Search for multiple things in a string
    ... >> I also feel that Regular Expressions, being an object in asp.net (not ... So using Regex is not really like using another language (as C# is different ... I agree with you that readability is important. ... And I was not saying experiment with it. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: for a laught (???)
    ... Regex doesn't work too well with a null byte delimiter :-) ... the API for a particular form of regular expressions ... Regex doesn't work with null terminated strings. ... qualifier or the qualifier "commonly" might have suggested. ...
    (comp.lang.cobol)