The application of regular expression in network programming (1)

Source: Internet
Author: User
Tags expression range regular expression valid email address
Programming | network | When we write web programs, we often judge the validity of a string, such as whether a string is a number, whether it is a valid email address, and so on. If you do not use regular expressions, then the judgment of the program will be very long and error prone, if the use of regular expressions, these judgments is a very easy job. This paper introduces the concept and format of regular expressions in a comprehensive way. And in the PHP, ASP application Examples to increase the reader's perceptual knowledge. Regular expressions are widely used and need to be summed up in learning and practice.
Introduction of regular expressions

In short, regular expressions are a powerful tool that can be used for pattern matching and substitution. A wide range of applications in network programming, such as the PHP scripting language or JavaScript, VBScript, and other client script languages provide support for regular expressions. Thus, regular expressions have gone beyond the limits of a language or a system and become a widely accepted concept and function.

Regular expressions allow the user to build a matching pattern by using a series of special characters, then compare the matching pattern with the target objects such as data file, program input, and form input of the Web page, and execute the corresponding program according to whether the matching pattern is included in the comparison object.

For example, one of the most common applications of regular expressions is to verify that the email address entered by the user is correctly formatted, and that if the user's email address is properly formatted through a regular expression, the form information that the user fills out will be processed properly; If the user enters a message address that does not match the pattern of the regular expression, a prompt will pop up asking the user to re-enter the correct e-mail address. This shows that regular expressions play an important role in the logical judgment of Web applications. We will give an example in detail later.

Regular expressions are generally like:/love/, where the part of the "/" delimiter is the pattern that will be matched in the target object. The user simply puts the pattern content that wants to find the matching object in between the "/" delimiters. Regular expressions provide specialized "meta characters" to enable users to customize schema content more flexibly. The term "metacharacters" refers to those special characters that have special meaning in regular expressions and can be used to specify the mode in which the leading character (that is, the character at the front of the metacharacters) appears in the target object. The more commonly used meta characters include: "+," *,? and {} ", or" \s,\s,\d,\w and \w ", and so on. In order to make the user more flexible to set the matching pattern, the regular expression allows users to use [] to define a character that matches a range of characters in a matching pattern instead of being limited to specific characters.

In addition to our meta characters, the regular expression also has another, more unique, special character, the locator. The locator character is used to specify where the match pattern appears in the target object. The more commonly used locator characters include: "^", "$", "\b" and "\b".

If we want to implement a "or" operation in a regular expression that is similar in programming logic, you can use the pipe character "|" If you choose one of several different modes to match. For example:

There is also a more commonly used operator in the regular expression, that is, the negative character "[^]". Unlike the locator "^" we mentioned earlier, the negative character "[^]" stipulates that the string specified in the pattern cannot exist in the target object. Generally, when "^" appears inside "[]", it is regarded as a negation operator, and when "^" is outside "[]" or there is no "[]", it should be treated as a locator character.

Finally, the escape character "\" is used when the user needs to add metacharacters to the pattern of regular expressions and find their matching objects. For example:/th\*/, the regular expression will match the "th*" in the target object rather than the "the".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.