Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to interpret the mailbox regular expression ^\ w+ ([- +.]\ w+) * @\ w+ ([-.]\ w+) *.\ w+ ([-.]\ w+) * $

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to interpret the mailbox regular expression ^\ w+ ([- +.]\ w+) * @\ w+ ([-.]\ w+) *.\ w+ ([-.]\ w+) * $. The editor thinks it is very practical, so I will share it for you as a reference. I hope you can get something after reading this article.

Verify the regular expression of the mailbox

Var ePattern = / ^ ([A-Za-z0-9 _\ -\.]) + @ ([A-Za-z0-9 _\ -\.]) +\. ([A-Za-z] {2heli4}) $/

Or

\ w + ([- +.]\ w +) * @\ w + ([-.]\ w +) *.\ w + ([-.]\ w +) *

\ w any uppercase and lowercase English letter 0-9 numeric underscore + means at least one character appears

[- +.]\ w + any include-+. The combination of\ w and\ w characters occurs 0 or more times, mainly including jb51.net in mailboxes such as jb51.net@vip.163.com

@ fixed symbol

\ w + characters that appear at least once\ w

[-.]\ w + characters that are combined zero or more times

For example: vip.163 in jb51.net@vip.163.com

\. Fixed symbols must include one of these.

Anyway, the combination of\ w + ([-.]\ w +) * is specified to start with a character of type\ w, followed by a\ w and-underscore. The combination of dots.

The whole sentence means

The email address must begin with an uppercase and lowercase letter or a number or an underscore, followed by any\ w character and the underscore plus sign @ followed by any\ w character and the underscore plus sign. Follow any\ w characters and the middle underscore plus sign.

Effect picture

In fact, you just need to look at the basics. Think carefully. Students who want to improve must always write by hand to see if they are different from those written by others.

Here are some additions

In a nutshell, regular expressions are a powerful tool for pattern matching and substitution. Regular expressions can be found in almost all UNIX-based tools, such as vi editors, Perl or PHP scripting languages, and awk or sed shell programs. In addition, client-side scripting languages like JavaScript also provide support for regular expressions. Thus it can be seen that regular expressions have gone beyond the limitations of a certain language or system and become a widely accepted concept and function.

Regular expressions allow users to build matching patterns by using a series of special characters, then compare the matching patterns with target objects such as data files, program inputs, and form inputs on WEB pages, and execute the corresponding programs according to whether the matching patterns are included in the comparison objects.

For example, one of the most common applications of regular expressions is to verify that the email address entered by the user online is in the correct format. If you verify that the format of the user's email address is correct through the regular expression, the form information filled in by the user will be processed normally; otherwise, if the email address entered by the user does not match the pattern of the regular expression, a prompt will pop up, requiring the user to re-enter the correct email address. Thus it can be seen that regular expressions play an important role in the logical judgment of WEB applications.

Basic grammar

After we have a preliminary understanding of the function and function of regular expressions, let's take a specific look at the grammatical format of regular expressions.

Regular expressions generally take the following form:

/ love/

The part between the "/" delimiters is the pattern that will be matched in the target object. Users only need to put the content of the pattern that they want to find a match between the "/" delimiters. In order to give users more flexibility to customize pattern content, regular expressions provide special "metacharacters". Metacharacters refer to those special characters with special meaning in regular expressions, which can be used to define the occurrence pattern of their leading characters (that is, characters in front of metacharacters) in the target object.

The more commonly used metacharacters include "+", "*", and

"?". Among them, the "+" metacharacter stipulates that its leading character must appear one or more times in the target object, the "*" metacharacter stipulates that its leading character must appear zero or several times in a row in the target object, and "?" The metacharacter states that its leading object must appear zero or once in a row in the target object.

Next, let's take a look at the specific application of regular expression metacharacters.

/ fo+/

Because the above regular expression contains the "+" metacharacter, it means that it can match strings such as "fool", "fo", or "football" in the target object where one or more letters o appear after the letter f in succession.

/ eg*/

Because the above regular expression contains the "*" metacharacter, it means that it can match strings such as "easy", "ego", or "egg" in the target object that have zero or more letters g after the letter e in succession.

/ Wil?/

Because the above regular expression contains "?" Metacharacters that can match "Win" or "Wilson" in the target object, such as a string with zero or one letter l after the letter I.

In addition to metacharacters, users can specify exactly how often patterns appear in matching objects. For example,

/ jim/

The above regular expression specifies that the character m can appear 2-6 times in a row in the matching object, so the above regular expression can match strings such as jimmy or jimmmmmy.

Now that we have a preliminary understanding of how to use regular expressions, let's take a look at a few other important meta-words

The way the character is used.

\ s: used to match a single space character, including the tab key and newline characters

\ s: used to match all characters except a single space character

\ d: used to match numbers from 0 to 9

\ w: used to match alphabetic, numeric or underscore characters

\ W: used to match all characters that do not match\ w

. Used to match all characters except newline characters

(note: we can think of\ s and\ S and\ w and\ W as inverse operations for each other)

Next, let's take a look at how to use the above metacharacters in regular expressions through an example.

/\ swords /

The above regular expression can be used to match one or more space characters in the target object.

/\ d000 /

If we have a complex financial statement on hand, we can easily find all the sums totaling thousands of yuan through the above regular expression.

In addition to the metacharacters we introduced above, there is another unique special character in regular expressions, the locator. The locator is used to specify where the matching pattern appears in the target object.

The more commonly used locators include "^", "$", "\ b" and "\ B". The "^" locator specifies that the matching pattern must appear at the beginning of the target string, the "$" locator specifies that the matching pattern must appear at the end of the target object, and the\ b locator states that the matching pattern must appear at one of the two boundaries at the beginning or end of the target string. The "\ B" locator states that the matching object must be within the beginning and end of the target string. That is, the matching object can be used neither as the beginning nor the end of the target string. Similarly, we can think of "^" and "$" and "\ b" and "\ B" as two sets of locators that inverse each other. For example:

/ ^ hell/

Because the above regular expression contains the "^" locator, it can match a string in the target object that starts with "hell", "hello" or "hellhound".

/ ar$/

Because the above regular expression contains a "$" locator, it can match a string in the target object that ends with "car", "bar" or "ar".

/\ bbom/

Because the above regular expression pattern begins with a "\ b" locator, it can match a string in the target object that starts with "bomb" or "bom".

/ man\ b /

Because the regular expression pattern above ends with a "\ b" locator, it can match a string in the target object that ends with "human", "woman" or "man".

In order to facilitate the user to set the matching pattern more flexibly, the regular expression allows the user to specify a certain range in the matching pattern without being limited to specific characters. For example:

/ [Amurz] /

The above regular expression will match any uppercase letter in the range A to Z.

/ [aMusz] /

The above regular expression will match any lowercase letter in the range a to z.

/ [0-9] /

The above regular expression will match any number in the range from 0 to 9.

/ ([a murz] [Amurz] [0-9]) + /

The above regular expression will match any string of letters and numbers, such as "aB0" and so on. One thing to remind users here is that you can use "()" in regular expressions.

Combine strings together. The contents of the "()" symbol must appear in the target object at the same time. Therefore, the above regular expressions will not be able to match with regular expressions such as

Strings such as "abc" match because the last character in "abc" is a letter rather than a number.

If we want to implement an "OR" operation similar to the "OR" operation in programming logic in regular expressions, we can use the pipe character "|" if we choose one of several different patterns to match. For example:

/ to | too | 2 /

The above regular expression will match "to", "too", or "2" in the target object.

There is also a more common operator in regular expressions, the negative character "[^]". Unlike the locator "^" we introduced earlier, the negative character

"[^]" states that the string specified in the schema cannot exist in the target object. For example:

/ [^ Amurc] /

The above string will match any character in the target object except Aline B and C. In general, "^" is considered a negative operator when it appears within "[]", and should be treated as a locator when "^" is outside "[]" or when there is no "[]".

Finally, the escape character "\" can be used when the user needs to add metacharacters to the pattern of the regular expression and find its match. For example:

/ Th\ * /

The above regular expression will match "Th*" instead of "The" in the target object.

Use an example

Now that we have a more comprehensive understanding of regular expressions, let's take a look at how to use regular expressions in Perl,PHP and JavaScript.

In general, regular expressions are used in the following format in Perl:

Operator / regular-expression / string-to-replace / modifiers

An operator can be m or s, which represents a matching operation and a replacement operation, respectively.

Among them, a regular expression is a pattern that will be matched or replaced, and can be composed of any character, metacharacter, or locator, etc. A replacement string is a string that replaces the found pattern matching object when the s operator is used. The final parameter item is used to control different ways of matching or replacing. For example:

S/geed/good/

The first occurrence of the geed string is found in the target object and replaced with good. If we want to perform multiple find-and-replace operations within the global scope of the target object, we can use parameters

"g" is s/love/lust/g.

In addition, we can use the parameter "I" if we don't need to limit the case of the match. For example,

M/JewEL/i

The above regular expression will match the jewel,Jewel, or JEWEL in the target object.

In Perl, use the special operator "= ~" to specify the matching object of the regular expression. For example:

$flag = ~ s/abc/ABC/

The above regular expression will replace the string abc in the variable $flag with ABC.

Next, we will add regular expressions to the Perl program to verify the validity of the user's e-mail address format.

The code is as follows:

#! / usr/bin/perl # get input print "What's your email address?\ n"; $email = chomp ($email); # match and display result if ($email = ~ / ^ ([a-zA-Z0-9 steps -]) + @ ([a-zA-Z0-9 steps -]) + (\. [a-zA-Z0-9 steps -]) + /) {print ("Your email address is correct!\ n") } else {print ("Please try again!\ n");}

If the user prefers PHP, you can use the ereg () function for pattern matching. The format of the ereg () function is as follows:

Ereg (pattern, string)

Where pattern represents the pattern of the regular expression, and string is the target object for the find and replace operation. Also to verify the email address, the program code written in PHP is as follows:

Finally, let's take a look at JavaScript. JavaScript

There is a powerful RegExp () object that can be used to match regular expressions. The test () method verifies that the target object contains a matching pattern and returns true or false accordingly.

We can use JavaScript to write the following script to verify the validity of the email address entered by the user.

Once you have a variable that contains the entire string, you can use regular expressions to manipulate the entire file

Instead of operating on a block in the file. There are two useful regular expression tags / s and / m. In general, Perl's regular expressions deal with lines, and you can write:

Undef $/; $line =; if ($line = ~ / (b.*grass) $/) {print "found $1\ n";}

If you fill in our documents as follows: browngrass

Bluegrass

The output is:

Found bluegrass

It does not find "browngrass" because $only looks for its match at the end of the string (or the line before the end of the string). If you are in a string that contains many lines, use "^"

To match with "$", we can use the / m ("multiline") option:

If ($line = ~ / (b.*grass) $/ m) {}

Now the program will output the following information:

Found browngrass

Similarly, a period can match all characters except newline characters:

While () {

If (/ 19 (. *) $/) {

If ($1 < 20) {

$year = 2000 years 1

} else {

$year = 1900 years 1

}

}

}

If we read "1981" from the file, $_ will contain "1981\ n". Periods in regular expressions match "8" and "1", not "\ n". This is what you need to do here, because the newline character is not part of the date. For a string that contains many lines, we may want to extract large blocks that may span line delimiters. In this case, we can use the / s option and use a period to match all characters except newline characters.

If (m {(. *?)} s) {

Print "Found bold text: $1\ n"

}

Here, I use {} to indicate the beginning and end of the regular expression instead of a slash, so I can tell Perl that I am matching with the start character "m" and the end character "s". you

You can combine the / s and / m options:

If (m {^ (. *?)} sm) {

#...

}

This is the end of the article on "interpreting the regular expression of mailbox ^\ w+ ([- +.]\ w+) * @\ w+ ([-.]\ w+) *.\ W+ ([-.]\ w+) * $" this is the end of the article. I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report