Saturday, July 24, 2010

Learning regular expressions entirely Manual: Getting Started guide rookie



Regular expressions can be pretty scary, very real terrorist. Fortunately, once expressed to remember the meaning of each symbol, fear will be quickly dissipated. If you know anything about regular expressions, as the title of the article, then you have a lot to learn. Let us get started.

Section I: Basic Learning

Java regular expression queries to achieve the conditions of

Regular expression group Xiangjie

3 minutes to let you know what regular expressions cited VB.NET ..

Three minutes to learn PHP regular expressions

Interpretation of multiple lines of PHP regular expression matching the code shown ..

Want to efficiently learn and master the regular expression, the key is to spend a day to remember all the symbols. This is probably the best I can offer suggestions. To sit down and do some memory cards, and then remember them. The following is the most common symbols:

. - Matches any character, except newline (if dotall is false).

* - The symbol of the preceding character, match 0 or more times.

+ - The symbol of the preceding character, match 1 or more times

? - The symbol of the preceding character is optional. Match 0 or 1.

d - matches any single number.

w - matches any character (including alphanumeric and underscore).

[XYZ] - matches any character set in a character, that is, X, Y, Z in any one.

[XYZ] + - match the character set in one or more characters.

$ - Matches the end of the string position.

^ - Matches the beginning of the string position.

[^ Az] - When in a character class, the ^ that NOT (non); for the sample that matches any non-lowercase letters.

Boring bar, but still remember them, remember that you will know after benefits.

Tool

Do you think an expression is correct, very correct, but just can not get the desired results, then you may have hair Baguang impulses. RegExr desktop application to download it, it is essential to you, and play together very interesting. It provides real-time checks, as well as a sidebar, which contains the definition of each character and the user, very detailed.






Section II: Regular expression fool Tutorial: screenshots video

The next step is to learn how to really use these symbols. If video is your preference, you luck. There are five courses of video tutorials, ideal for you: "regular expression tutorial fool."






(Jeffery Way: In this series of video tutorials, I will give you how JavaScript and PHP in the efficient use of regular expressions. I will assume you are starting from scratch.)

Section III: Regular Expressions and JavaScript

This section is the last section, we look at methods of how to use JavaScript regular expressions.

1. Test ()

This method takes a single string parameter and returns a Boolean value indicating whether to find a criticism. If you do not need to match the results of a particular operation, for example, verify the user name, "test" method has been enough to complete this task.

Example

var username = 'JohnSmith'; alert (/ [A-Za-z_-] + /. test (username)); / / returns true

In the above code, we first declare a regular expression that allows only uppercase or lowercase letters, underscores and hyphens. Acceptable character of these brackets, you specify a character set. Followed by the + sign indicates that we want are looking for is one or more of the aforementioned character group of characters. And then use the paradigm of the variable "JohnSmith" test. Because of matching, the browser display box will show true.

2. Split ()

You may be very familiar with the split method has been. The method accepts a single regular expression, that where a "split." Please note that if you like, you can also use the string.

var str = 'this is my string'; alert (str.split (/ s /)); / / alerts "this, is, my, string"

S in the code above that a single space, through which we will split into an array of strings. If you want to visit a particular value, you can use the corresponding index.

var str = 'this is my this string'; alert (str.split (/ s /) [3]); / / alerts "string"

3. Replace ()

You may have thought, replace method can be used as part of the text in (by the string or regular expression that) replaced with a different string.

Example

If you want to "Hello, World" into "Hello, Universe", you can use the following code:

var someString = 'Hello, World'; someString = someString.replace (/ World /, 'Universe'); alert (someString); / / alerts "Hello, Universe"

Should be noted that, for this simple example, we could simply use. Replace ('World', 'Universe'). In addition, the use replace method does not automatically override the value of the variable, we must once again return value assigned to the variable: someString.

Example 2

As another example, suppose the user to register an account on our site, we may want to provide some basic safety precautions. Maybe we want to leave their user name, and remove any other symbols, quotation marks, semicolons, etc.. JavaScript and regular expressions for the implementation of these tasks is trivial little things pile.

var username = 'J; ohnSmith ;@%'; username = username.replace (/ [^ A-Za-zd_-] + /,''); alert (username); / / JohnSmith; @%

The last generation to see the display value, some may think the above code is wrong. This is not true. You look, you will find the letter "J" was deleted after the semicolon, as we expect. To tell the engine to continue the search string to find more matches, we can do directly behind the end of the slash to add a "g", the modifier or mark that "global (overall)." The revised code is as follows:

var username = 'J; ohnSmith ;@%'; username = username.replace (/ [^ A-Za-zd_-] + / g,''); alert (username); / / alerts JohnSmith

Now, regular expression search the entire string, replace all the necessary characters. Let us look at the key expression (. Replace (/ [^ A-Za-zd_-] + / g,'');), to note that the up arrow in brackets (ie ^) is very important. When on the character group, the symbol "find all is not ... ...." Now back in to see the code, it said, to find all is not letters, numbers (indicated by the d), underscore or hyphen symbol; If a match is found, replace the air, in fact, is to remove the character.

4. Match ()

And test different methods, match () returns a array of all the criticism found.

Example

var name = 'JeffreyWay'; alert (name.match (/ e /)); / / alerts "e"

The above code will display a letter "e". However, the string "JeffreyWay" in fact contains two e. Similarly, we have to use the modifier "g" to declare a global search.

var name = 'JeffreyWay'; alert (name.match (/ e / g)); / / alerts "e, e"

If you want to show that these specific values in the array A, can be referenced in brackets do you want to index.

var name = 'JeffreyWay'; alert (name.match (/ e / g) [1]); / / alerts "e"

Example 2

Let's see next example, to ensure that our understanding of it is correct.

var string = 'This is just a string with some 12345 and some! @ # $ mixed in.'; alert (string.match (/ [az] + / gi)); / / alerts "This, is, just, a, string, with, some, and, some, mixed, in "

In this regular expression, we created a paradigm, can match one or more uppercase or lowercase letters. Thanks to the "i" modifier. Except, we also added the "g" to declare global search. The above code will display "This, is, just, a, string, with, some, and, some, mixed, in." Then, if you want to access variables in the array in one of these values, we need only quote the corresponding The index can be.

var string = 'This is just a string with some 12345 and some! @ # $ mixed in.'; var matches = string.match (/ [az] + / gi); alert (matches [2]); / / alerts "just"

Separate e-mail address

To practice, we try to an e-mail address (nettuts@tutsplus.com) divided into two corresponding parts: user name and domain name, ie nettuts and tutsplus.

var email = 'nettuts@tutsplus.com'; alert (email.replace (/ ([a-zd_-]+)@([ a-zd_-]+).[ az] (2,4) / ig, ' $ 1, $ 2 ')); / / alerts "nettuts, tutsplus"

If the regular expression, you're the new kids, the above code may seem a little scary. Do not worry, for the first time will feel that "terrorist." Once it is broken down into one of a small subset, you will find really very simple. Let us analyze each one:

. Replace (/ ([a-zd_-] +)

From the middle of view, we want to search for any letters, numbers, underscores or hyphens, and match one or more times (+). Whether matching is above the value we want to visit, so to put in parentheses. In this way, we will later apply this matching subset.

@ ([A-zd_-] +)

Immediately before a match, we see that @ symbol, then there is a group of one or more letters, numbers, underscores and hyphens. Similarly, we will put it in brackets, for later access.

. [A-z] (2,4) / ig,

Keep looking, we see a point. Because the regular expression, the period can represent any character (except newline at times), so you must use "" to escape. The last section is used to find ". Com". We know that most of the domain name, if not all, the suffix 2 to 4 characters (com, edu, net, name, etc.). If you find the specific range, we can first use a more conventional symbols, such as backup bin conf config data eshow_sitemap.html generate.sh log maint sitemap.html svn tmp or +. However, we are here is to put a big number 2 in parentheses, maximum and minimum values respectively.

'$ 1, $ 2')

The last part of the methods that replace the second argument, or we will match the character set you want to replace the object. Here, we use $ 1 and $ 2 respectively, to reference stored in the first and second values in parentheses. For this particular example, the $ 1 point nettuts, $ 2 point tutsplus.

The location of objects to create your own

As a final project, we will create the position of the object. Position objects to provide users with information about the current page: href, protocol, address, port, etc.. Please note that here only as an exercise. For a real web site, you can use the existing location of the object.

First we create the position function, the function accepts a single parameter that we want to "decode" the web site, we will call it "loc".

function loc (url) ()

Now we can call it the following way, and pass a mess url:

var l = loc ('http://www.somesite.com?somekey=somevalue&anotherkey=anothervalue # theHashGoesHere');

Next, we need to return the object that contains multiple methods.

function loc (url) (return ())

Search (search)

We will not create all the methods, but we will copy a few. The first is the "search". Use regular expressions, we will search url and return the entire contents of the query string.

return (search: function () (return url.match (/?(.+)/ i) [1]; / / returns "somekey = somevalue & anotherkey = anothervalue # theHashGoesHere"))

In the above code, we use the incoming url, try to use our regular expression to match his. The regular expression search string in the question mark, question mark indicates that the query string (querystring) start. In this position, we need to Austria for the rest of the characters, which is the (. +) Into the brackets. Finally, we need to return to the character blocks, hence the use of [1] to locate it.

Hash (Hash)

Now we will create a method, returns url's hash value, or the # contents of the back.

hash: function () (return url.match (/#(.+)/ i) [1]; / / returns "theHashGoesHere"),

This time, we search for the # sign, the same, get behind the characters using the brackets and use [1] point to that specific subset.

Agreement

protocol method should return, the agreement used by the page, you may have guessed. Such agreements usually http or https.

protocol: function () (return url.match (/ (ht | f) tps?: / i) [0]; / / returns 'http:'),

This is a slightly more complicated, because there are several options: http, https and ftp.

Although you can use this format (http | https | ftp), but using (ht | f) tps? More concise, that we first find the "ht" or "f" character, the next step, match the "tp" character. The final "s" is optional, so we added a question mark, question mark before the character that appears zero or one.

Href

This is the last method, returns the page url.

href: function () (return url.match (/(.+.[ az] (2,4)) / ig); / / returns "http://www.somesite.com")

Here, we match all the characters until you find a dot, the point behind No. 2 - 4 characters (for com, au, edu, name, etc.). Important is aware that these expressions, we can write very complex and can write very simple, we ask how this is rigorous.

A simple function

function loc (url) (return (search: function () (return url.match (/?(.+)/ i) [1];), hash: function () (return url.match (/#(.+ ) / i) [1];), protocol: function () (return url.match (/ (ht | f) tps ?:/)[ 0];), href: function () (return url.match (/ (.+.[ az] (2,4)) / ig);)))

Use this function above, we could be very simple to show each part of the web site:

var l = loc ('http://www.net.tutsplus.edu?key=value # hash'); alert (l.href ()); / / http://www.net.tutsplus.com alert ( l.protocol ()); / / http:...etc.







Recommended links:



Bill - Bill Gates retirement after pitching in a number of patent applications



"Do not call me"



My favorite News Servers



Total Video Converter



Christmasgift DVD Editor



On the Implementation of financial knowledge in the use of



Catalogs Games Card



Pursuit Of "simple Simple" The ORM Mapping Framework [2]



Silver DVD to Zune Converter



Easily create bar codes



video Format for ps3



The Open Group President of Beijing University speech



Recommend Wizards And Components



swf files



.swf file



No comments:

Post a Comment