Loading ...
Sorry, an error occurred while loading the content.

Re: [hackers-il] Re: Toward YARET (Yet Another Regular Expressions Tutorial)

Expand Messages
  • Gabor Szabo
    ... Do you say a regex is more efficient and faster than substr? Have you benchmarked the two solutions? ... Usually implementation of a function should not be
    Message 1 of 7 , Jan 3, 2006
    • 0 Attachment
      On 1/2/06, ik_5 <idokan@...> wrote:
      > Well, substr is the first of all "old school". I found that
      > developers that learn that perl (for example) have the substr function
      > in a language, will use it more often then regex, even when regex is
      > needed (speed and efficiency as two examples of a need).

      Do you say a regex is more efficient and faster than substr?
      Have you benchmarked the two solutions?

      > Secondly, the last time I checked, substr in perl (you more then
      > welcome to tell me that I'm wrong :)) used regex by itself. so
      > theoretically speaking, I think that using the regex inside substr is
      > better, then to use the substr function.

      Usually implementation of a function should not be a factor in decision making.
      After all no one guaranties that the implementation won't change.
      That said. In what terms is this solution "better".
      IMHO the regex version is less readable but YMMV and TMTOWTDI.

      > Beside that, I found that solving such things using regex, actually
      > teaches you more about the regex language, then using the substr. And
      > all that I gave (with extra slash that I see now) was a PoC of what I
      > would expect in regex tutorial that comes to teach me how to use regex
      > with many divert problems as possible. Because I find it missing on
      > all of the documents I ever read and used.

      For teaching purposes it *might* be good. It might help people
      understand the result of certain regular expressions. I myself use
      similar examples in some of my materials - that is implementing
      the functionality of a built in operator with other tools - but I think
      I will remove them from my courese.

      It nearly always confuses people why do they need to implement some
      thing that already exists in the language.

      Here is another example:

      $name = s/(.*)/$1$1$1$1$1$1/s;

      That's a perfect regex.
      Wouldn't this be more readable:
      $name = $name x 6;

      Or maybe
      $name x= 6;

      > Regardsing to ".", the char tells you "find a char". if I want to find
      > all of the string between the beginning of a line to the end, when I
      > give the first and last words (for example), I find the use of ".*?"
      > working much better then ".*".

      I am not sure how is this related.


      > Ido
      > --- In hackers-il@yahoogroups.com, Gabor Szabo <szabgab@g...> wrote:
      > >
      > > While this was not the real question but
      > >
      > > > Or things like:
      > > > (Perl regex)
      > > >
      > > > $var =~ s//^(.{10}).*$/$1/;
      > >
      > > Why would you write such a regular expression ?
      > > Why not $var = substr($var, 0, 10);
      > > But even if you want to use regexes, the .*$ is unnecessary there.
      > > (set aside the minor issue that . does not match newlines)
      > >
      > > I understand that the question was not about this specific regex
      > > but sometimes I feel people are overusing regular expressions.
      > >
      > > Gabor
      > >
      > Yahoo! Groups Links

      Gabor Szabo
      Perl in Israel http://www.pti.co.il
      08-975-2897 054-4624648 AIM: gabor529
    Your message has been successfully submitted and would be delivered to recipients shortly.