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

What are the contents of regular expressions

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article is about what regular expressions are about. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

What is a regular expression?

To put it simply: regular expression (Regular Expression) is a language that handles string matching.

A regular expression describes a pattern of string matching that can be used to check whether a string contains a substring and perform a "fetch" or "replace" operation on the matched substring.

II. Application of regular expressions

Regular expressions are very practical in the actual development process, and can quickly solve some complex string processing problems. Here are some simple classifications for the application of regular expressions:

First: data validation

For example, if you want to verify whether a string is the correct EMail,Telphone,Ip, and so on, it is very convenient to use regular expressions.

The second kind: content search

For example, if you want to grab a picture of a web page, you must find it.

Tag, which can be accurately matched with regular expressions.

The third kind: content replacement

For example, if you want to hide the middle four digits of your mobile phone number into this pattern, 123 regular expressions 4567, then using regular expressions will also be very convenient.

What are the contents of regular expressions

Next, I will briefly introduce regular expressions:

1. Several important concepts of regular expressions

Subexpression: in a regular expression, if you use the content enclosed by "()", it is called "subexpression".

Capture: the result matched by the subexpression is placed in a buffer by the system, a process we call "capture".

Reverse reference: we use "\ n", where n is a number, indicating the content between previous buffers, which we call "back reference".

2. Quantity qualifier

X+ means: 1 or more

X* indicates: 0 or more

X? Indicates: 0 or 1

X {n} means: n

X {n,} means: at least n

X {nrecom} means: n to m, greedy principle, will match as many as possible; if you add a? Is the principle of non-greed

Note: X represents the character you are looking for

3. Character qualifier

\ d means: matches a numeric character, [0-9]

\ D means: matches a non-numeric character, [^ 0-9]

\ w means: matches word characters including underscores, [0-9a murzAmurz]

\ W means: match any non-word character, [^ 0-9aMIZAMIZZ]

\ s means: match any white space characters, spaces, carriage returns, tabs

\ s means: matches any non-white space character

. means: matches any single character

In addition, there are the following:

Range characters: [Amurz], [Amurz], [0-9], [0-9a-z], [0-9a-zA-Z]

Any character: [abcd], [1234]

Non-inner characters: [^ amurz], [^ 0-9], [^ abcd]

4. Locator

^ indicates: beginning identification

$means: end mark

\ b means: word boundary

\ B means: non-word boundary

5. Escape character

\ used to match some special characters

6. Select a match

| | multiple rules can be matched |

7. Special usage

(? =): forward preview: matches a string that ends with the specified content

(?!): negative pre-check: matches a string that does not end with the specified content

(?:): do not put the contents of the selected match in the buffer

Fourth, the use of regular expressions under Javascript.

There are two ways to use regular expressions under Javascript:

The first method: use the RegExp class

The methods provided are:

Test (str): returns true/false when the string matches whether there is a string matching the pattern

Exec (str): returns the string to which the matching pattern is matched, if any, returns the corresponding string, none, and returns null

/ / if there is a subexpression in the regular expression, when using the exec method

/ / return: result [0] = matching result, result [1] = subexpression 1 matching result.

The second way is to use the String class

The methods provided are:

Search: returns the location of the string that matches the pattern, or-1 if not

Match: returns the string to which the matching pattern is matched. If any, an array is returned. None. Null is returned.

Replace: replace the string to which the matching pattern is matched

Split: separates strings with matched patterns as delimiters and returns an array

Fifth, the use of regular expressions under PHP.

There are two functions that use regular expressions under PHP:

The first is: Perl regular expression function

The methods provided are:

Preg_grep-returns array units that match the pattern

Preg_match_all-- perform global regular expression matching

Preg_match-- regular expression matching

Preg_quote-escape regular expression characters

Preg_replace_callback-search and replace regular expressions with callback functions

Preg_replace-perform search and replacement of regular expressions

Preg_split-- splits strings with regular expressions

The second is: POSIX regular expression function

The methods provided are:

Ereg_replace-- replace regular expressions

Ereg-regular expression matching

Eregi_replace-case-insensitive replacement of regular expressions

Eregi-case-insensitive regular expression matching

Split-- splits a string into an array using regular expressions

Spliti-splits a string into an array using regular expressions that are case-insensitive

Sql_regcase-produces regular expressions for size-insensitive matches

Thank you for reading! This is the end of this article on "what regular expressions have". 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, you can 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