Jun 19, 2011

Regular Expressions as a Language

Unless you've had some experience with regular expressions, you won't understand the regular expression ^(From|Subject): from the last example, but there's nothing magic about it. For that matter, there is nothing magic about magic. The magician merely understands something simple which doesn't appear to be simple or natural to the untrained audience. Once you learn how to hold a card while making your hand look empty, you only need practice before you, too, can "do magic." Like a foreign language once you learn it, it stops sounding like gibberish.

1.2.1. The Filename Analogy

Since you have decided to use this book, you probably have at least some idea of just what a "regular expression" is. Even if you don't, you are almost certainly already familiar with the basic concept.
You know that report.txt is a specific filename, but if you have had any experience with Unix or DOS/Windows, you also know that the pattern "*.txt" can be used to select multiple files. With filename patterns like this (called file globs or wildcards), a few characters have special meaning. The star means "match anything," and a question mark means "match any one character." So, with the file glob "*.txt," we start with a match-anything * and end with the literal .txt, so we end up with a pattern that means "select the files whose names start with anything and end with .txt".
Most systems provide a few additional special characters, but, in general, these filename patterns are limited in expressive power. This is not much of a shortcoming because the scope of the problem (to provide convenient ways to specify groups of files) is limited, well, simply to filenames.
On the other hand, dealing with general text is a much larger problem. Prose and poetry, program listings, reports, HTML, code tables, word lists... you name it, if a particular need is specific enough, such as "selecting files," you can develop some kind of specialized scheme or tool to help you accomplish it. However, over the years, a generalized pattern language has developed, which is powerful and expressive for a wide variety of uses. Each program implements and uses them differently, but in general, this powerful pattern language and the patterns themselves are called regular expressions.

1.2.2. The Language Analogy

Full regular expressions are composed of two types of characters. The special characters (like the * from the filename analogy) are called metacharacters, while the rest are called literal, or normal text characters. What sets regular expressions apart from filename patterns are the advanced expressive powers that their metacharacters provide. Filename patterns provide limited metacharacters for limited needs, but a regular expression "language" provides rich and expressive metacharacters for advanced uses.
It might help to consider regular expressions as their own language, with literal text acting as the words and metacharacters as the grammar. The words are combined with grammar according to a set of rules to create an expression that communicates an idea. In the email example, the expression I used to find lines beginning with 'From:' or 'Subject:' was . The metacharacters are underlined; we'll get to their interpretation soon.
As with learning any other language, regular expressions might seem intimidating at first. This is why it seems like magic to those with only a superficial understanding, and perhaps completely unapproachable to those who have never seen it at all. But, just as !would soon become clear to a student of Japanese, the regular expression in
 "Regular expressions are easy!" A somewhat humorous comment about this: as Chapter 3 explains, the term regular expression originally comes from formal algebra. When people ask me what my book is about, the answer "regular expressions" draws a blank face if they are not already familiar with the concept. The Japanese word for regular expression, , means as little to the average Japanese as its English counterpart, but my reply in Japanese usually draws a bit more than a blank stare. You see, the "regular" part is unfortunately pronounced identically to a much more common word, a medical term for "reproductive organs." You can only imagine what flashes through their minds until I explain!

will soon become crystal clear to you, too.
This example is from a Perl language script that my editor used to modify a manuscript. The author had mistakenly used the typesetting tag <emphasis> to mark Internet IP addresses (which are sets of periods and numbers that look like The incantation uses Perl's text-substitution command with the regular expression

to replace such tags with the appropriate <inet> tag, while leaving other uses of <emphasis> alone. In later chapters, you'll learn all the details of exactly how this type of incantation is constructed, so you'll be able to apply the techniques to your own needs, with your own application or programming language. The goal of this book
The chance that you will ever want to replace <emphasis> tags with <inet> tags is small, but it is very likely that you will run into similar "replace this with that" problems. The goal of this book is not to teach solutions to specific problems, but rather to teach you how to think regular expressions so that you will be able to conquer whatever problem you may face.

Jun 18, 2011

Solving Real Problems

Knowing how to wield regular expressions unleashes processing powers you might not even know were available. Numerous times in any given day, regular expressions help me solve problems both large and small (and quite often, ones that are small but would be large if not for regular expressions).
Showing an example that provides the key to solving a large and important problem illustrates the benefit of regular expressions clearly, but perhaps not so obvious is the way regular expressions can be used throughout the day to solve rather "uninteresting" problems. I use "uninteresting" in the sense that such problems are not often the subject of bar-room war stories, but quite interesting in that until they're solved, you can't get on with your real work.
As a simple example, I needed to check a lot of files (the 70 or so files comprising the source for this book, actually) to confirm that each file contained 'SetSize' exactly as often (or as rarely) as it contained 'ResetSize'. To complicate matters, I needed to disregard capitalization (such that, for example, 'setSIZE' would be counted just the same as 'SetSize'). Inspecting the 32,000 lines of text by hand certainly wasn't practical.
Even using the normal "find this word" search in an editor would have been arduous, especially with all the files and all the possible capitalization differences.
Regular expressions to the rescue! Typing just a single, short command, I was able to check all files and confirm what I needed to know. Total elapsed time: perhaps 15 seconds to type the command, and another 2 seconds for the actual check of all the data. Wow! (If you're interested to see what I actually used, peek ahead to page 36.)
As another example, I was once helping a friend with some email problems on a remote machine, and he wanted me to send a listing of messages in his mailbox file. I could have loaded a copy of the whole file into a text editor and manually removed all but the few header lines from each message, leaving a sort of table of contents. Even if the file wasn't as huge as it was, and even if I wasn't connected via a slow dial-up line, the task would have been slow and monotonous. Also, I would have been placed in the uncomfortable position of actually seeing the text of his personal mail.
Regular expressions to the rescue again! I gave a simple command (using the common search tool egrep described later in this chapter) to display the From: and Subject: line from each message. To tell egrep exactly which kinds of lines I wanted to see, I used the regular expression ^(From|Subject):.
Once he got his list, he asked me to send a particular (5,000-line!) message. Again, using a text editor or the mail system itself to extract just the one message would have taken a long time. Rather, I used another tool (one called sed) and again used regular expressions to describe exactly the text in the file I wanted. This way, I could extract and send the desired message quickly and easily.
Saving both of us a lot of time and aggravation by using the regular expression was not "exciting," but surely much more exciting than wasting an hour in the text editor. Had I not known regular expressions, I would have never considered that there was an alternative. So, to a fair extent, this story is representative of how regular expressions and associated tools can empower you to do things you might have never thought you wanted to do.
Once you learn regular expressions, you'll realize that they're an invaluable part of your toolkit, and you'll wonder how you could ever have gotten by without them.If you have a TiVo, you already know the feeling!
A full command of regular expressions is an invaluable skill. This book provides the information needed to acquire that skill, and it is my hope that it provides the motivation to do so, as well.

Jun 14, 2011

Living With Pets May Protect Infants From Allergies

By Amanda Gardner
MONDAY, June 13, 2011 (Health.com) — Children who live with dogs and cats are less likely to develop allergies to those animals later in life, but only if the pet is under the same roof while the child is still an infant, a new study suggests.
Compared to babies born into cat-free homes, those who grew up with cats were roughly half as likely to be allergic to them as teenagers, the study found. Growing up around a dog reduced the risk of dog allergies by about the same amount for boys, but not for girls—a finding that mystified researchers.
Being exposed to pets anytime after the first year of life appeared to have no effect on allergy risk, however, which indicates that timing may be everything when it comes to preventing allergies.
Though they can’t say for sure, the researchers suspect that early exposure to pet allergens and pet-related bacteria strengthens the immune system, accustoms the body to allergens, and helps the child build up a natural immunity.
“Dirt is good,” says lead researcher Ganesa Wegienka, Ph.D., summing up the theory. “Your immune system, if it’s busy with exposures early on, stays away from the allergic immune profile.”
This isn’t the first study to find that having a household pet may protect kids from allergies, but it is the first to follow children until they were 18 years old.
Previous studies have had mixed results˜some have even linked pet exposure during infancy to an increased risk of allergy˜so it’s too early to recommend getting a dog or cat just to ward off allergies in your infant, says David Nash, M.D., clinical director of allergy and immunology at the Children’s Hospital of Pittsburgh.
“In the end, we’ll probably find out that there are periods of opportunity when exposure to allergens, for some people, is going to have a protective effect,” says Dr. Nash, who was not involved with the new study. “But we’re a long way from figuring out who it’s protective for and when that optimal period is.”
By the same token, don’t give away your beloved family pet because you’re concerned the critter will provoke allergies. “I would not get rid of my dog if I was having a child,” says Wegienka, an epidemiologist in the department of public health sciences at Henry Ford Hospital, in Detroit. “There’s no evidence that you should get rid of a dog or a cat.”
Moreover, it’s possible that factors other than having a dog or cat in the house influenced the study participants’ risk of allergy. For instance, although the researchers took into account whether the children’s parents were allergic to animals, they didn’t ask about a broader family history of allergies or other health problems. So it could be that children who are genetically predisposed to animal allergies simply are less likely to grow up in homes with pets.
In the study, which appears in the journal Clinical & Experimental Allergy, Wegienka and her colleagues collected information from 566 children and their parents about the kids’ exposure to indoor pets and their history of allergies. In addition, when the kids turned 18, the researchers took blood samples and tested them for certain immune-system proteins (known as antibodies) that fight off cat and dog allergens.
The teenagers who lived with a cat during their first year of life had a 48 percent lower risk of cat allergy than their peers, and the teen boys who lived with a dog had a 50 percent lower risk of allergy. The authors suggest that infant girls may not develop the same immunity as boys because they may interact differently with dogs than infant boys, but that’s only a guess.

Jun 13, 2011

Never accept a gift

A contractor for business reasons, prepared to use a new  luxury cars to bribe a member.

But member not happy and  said: "Sir, the usual codes of conduct and my own basic sense of honor, do not allow me to accept the gift! "

Contractor said: "Sir, I understand the position you are in, so be it, the price of the car is 10 dollars I sell you . "

Members to consider for a moment, replied emphatically: "That being the case, I'll buy two. "

Jun 12, 2011

Flight not yet reached

One professors like to talk to a dirty joke in class, female students have a great view, they agreed to if the professor joke revisit next class, they all the collective leave the classroom , to protest. The next day, the professor in class say a story: "I heard the strike of paris prostitutes,prostitutes buoyant......" His words have not finished, female students have stood up, ready to withdraw from the class. Professor look anxious and shout: "Wait! on the next flight to paris is 6 o'clock tomorrow morning. "

Jun 11, 2011

ugly girl

An ugly girl boat to cross the river with the monk, the monk inadvertently take a look at the ugly girl, ugly girl immediately lost his temper: "Bold bald, broad daylight   dare to peek women! "

Monk listen and scared
close eyes quickly. Ugly girl find that, more angry: "You looking at me  and i not say, dare to  close your eyes and  in your heart think me! "

Monk can not reason with her, twisting his face into other side. Ugly girl have reason not forgive people, hands akimbo, yelle: "You think there is no face to see me, goes to show you a guilty conscience! "

Jun 2, 2011


The football match is about to begin, and a newspaper reporter go to before one player who are doing pre-match preparation activities, and request him to talk to the enthusiastic fans about game hope.

The player think and say: "When I take the ball smoothly across each other's defense , rush to the goal area when ready to shoot, I hope the other goalie fell to the ground suddenly cramp. "