[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] What is regex



> A regular expression is a *description of
> a string*. 

No, it's a description of a *set of strings*.

Computer scientists refer to the | operator as `union', because that's
what it is.

So, Mr. Carlson, here is the idea behind regular expressions: They are
a compact notation for  sets of strings.  

Sets can be identified with properties: For example, the set of past
presidents of the US can be identified with the property of being a
past president of the US; computationally they are pretty much the
same thing, because the questions of whether someone is a past
president of the US or is a member of the set are exactly the same.

So you can use a regular expression to express a *property* that a
string might or might not have.  For example, the property of
beginning with the letter `w' is expressed by this regular expression:

	^w

Alternatively, you can consider it a notation that represents the set
of all strings beginning with a w, which is the same thing.

The property of being three characters long:

	^...$

The property of containing the word `nostril':

	nostril

Perl provides an operator, =~, which asks whether a particular string
belongs to the set, or has the property---these questions are the
same.  When you ask

	"what?" =~ /^w/

Perl asks ``Does the string `what?' begin with a `w'?'' and then the
answer is `yes'.  If you write

	if ($x =~ /nostril/) {
	  ...
	}

then Perl executes the exclosed block of code only if the string in $x
contains the word `nostril'.

The important thing about regular expressions is that the notation is
very compact and very powerful.  It can be used to compactly express
many important and common properties of strings.



==== Want to unsubscribe from this list? (Don't you love us anymore?)
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org