Subject: Toward YARET (Yet Another Regular Expressions Tutorial)

From: Omer Zak <[email-masked]>
Date: Mon, 02 Jan 2006 13:28:14 +0200

Someone asked me to help him learn regular expressions (used by egrep,
sed, awk, perl, python, ...).

So I am considering writing yet another tutorial about the subject.
Since there appear to be some tutorials about the subject, I'll
appreciate inputs about whether the existing tutorials are good enough
and what could I do differently so that my tutorial will be helpful to
those who did not succeed in mastering regular expressions using the
existing tutorials.

                                    Thanks,
                                         --- Omer
-- 
Sent from a PC running a top secret test version of Windows 97.
My own blog is at http://www.livejournal.com/users/tddpirate/

My opinions, as expressed in this E-mail message, are mine alone.
They do not represent the official policy of any organization with which
I may be affiliated in any way.
WARNING TO SPAMMERS:  at http://www.zak.co.il/spamwarning.html

---

From: guy keren <[email-masked]>
Date: Mon, 2 Jan 2006 13:42:20 +0200 (EET)

> those who did not succeed in mastering regular expressions using the

why not ask him which tutorials he read, and why they were not sufficient?

--
guy

"For world domination - press 1,
 or dial 0, and please hold, for the creator." -- nob o. dy

---

From: Omer Zak <[email-masked]>
Date: Mon, 02 Jan 2006 14:17:15 +0200

> why not ask him which tutorials he read, and why they were not sufficient?

I already asked him.

But he is only one "client", not enough to determine whether there is
enough demand to justify investing time in such a tutorial; and not
enough to select an approach which will help both him and other people.

-- 
One does not make peace with enemies.  One makes peace with former
enemies.
My own blog is at http://www.livejournal.com/users/tddpirate/

---

From: "ik_5" <[email-masked]>
Date: Mon, 02 Jan 2006 13:07:26 -0000

Hi,

I find that most regex tutorials covers very basic issues that will
not help a person that wishes to do something a bit more complex...

Thats include btw tricks.. for example while ereg support .* on perl
you can do .*?
.*? is less greedy then  .* and people should understand why etc.. 

Or things like:
(Perl regex)

 $var =~ s//^(.{10}).*$/$1/;

(trunk the string to have only the 10 first chars) etc ...

Groups and classes also almost never explained, except in their very
basic manners (what happen when i have nested groups, almost never
explained on tutorials for example).

You should also mention the difference between each regex engine you
are going to talk about etc...

There were more issues in the past (when I started learning regex),
but I can not remember at this moment. 

Ido

---

From: Gabor Szabo <[email-masked]>
Date: Mon, 2 Jan 2006 15:50:52 +0200

While this was not the real question but

>  $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

---

From: "ik_5" <[email-masked]>
Date: Mon, 02 Jan 2006 15:09:23 -0000

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).

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. 

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.

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 ".*". 

Ido

---

From: Gabor Szabo <[email-masked]>
Date: Tue, 3 Jan 2006 16:17:48 +0200

> 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?

> 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.

> 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;

> working much better then ".*".

I am not sure how is this related.

Gabor

> Yahoo! Groups Links

--
Gabor Szabo
Perl in Israel  http://www.pti.co.il
08-975-2897   054-4624648    AIM: gabor529