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 is the principle of regular expression and what are the common examples

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this issue, the editor will bring you about the principles of regular expressions and what are common examples. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

I. the meaning of regularity

A regular expression is a logical formula used to manipulate strings.

Second, the application scenario of regular expressions

Text filtering of data acquisition in data Analysis

The matching of web page data when crawling

Validation of user input data when writing front-end code

Testers validate the data of the request result

Batch text editing, such as Sublime Text or nodepad++, EditPlus and other notepad software, all support the use of regular expressions

Third, commonly used format verification

Mailbox verification

IP address verification

Phone number verification

ID card number verification

Password strength verification

URL verification

Chinese character verification [\ u4e00 -\ u9fa5]

.

Regular expressions can be used to acquire data in batches with certain rules.

Fourth, metacharacter description. Represents any character other than a newline character. The newline character is: (different operating systems have different newline characters)\ n\ r\ n\ w matches a character of letters, numbers, underscores, and Chinese characters\ s matches any blank character\ d matches the beginning of the string ^ matches the end of the string, antisense code

Antisense code means code that is the opposite of a metacharacter.

\ W matches any character that is not letters, numbers, underscores, or Chinese characters

\ s matches any character that is not a blank character

\ d matches any character that is not a number

6. The qualifier symbol indicates that * is repeated 0 times or multiple times + repeated 1 or multiple times? It means to repeat 0 times or 1 time {n} repeat n times, for example: {3} means to repeat 3 times {n,} repeat n times or more times {n ~ m} repeat n times to m times, so here m is generally better than n7, grouping matching.

Findall / search/match distinction

Findall is looking for all the

Search matches the first one

Match matches the beginning of

8. Greed and non-greed

Greed means to match as much as possible.

Non-greed means as few matches as possible.

Non-greedy operators are question marks: 'symbol?'

? The sign represents 0 or 1 repetition, plus a question mark, which represents a non-greedy operation, so the final match is only 0 times.

Branch condition matching:

Use | to separate different regular expressions, representing condition 1 or condition 2 or condition 3.

9. Zero width assertion

Matches the position before the regular expression reg (? = reg)

Matches the position after "regular expression reg" (? Common practical regular expressions

Enter zero and non-zero numbers: "^ (0 | [1-9] [0-9] *) $".

Enter a positive real number with two decimal places: "^ [0-9] + (. [0-9] {2})? $".

Enter a positive real number with 1-3 decimal places: "^ [0-9] + (. [0-9] {1Jing 3})? $".

Enter a non-zero positive integer: "^" +? [1-9] [0-9] * $".

Enter a non-zero negative integer: "^"-[1-9] [] 0-9 "* $.

Enter a character of length 3: "^. {3} $".

Enter a 26-letter string: "^ [A-Za-z] + $".

Enter a string of 26 uppercase letters: "^ [Amurz] + $".

Enter a string of 26 lowercase letters: "^ [a murz] + $".

Enter a string of numbers and 26 letters: "^ [A-Za-z0-9] + $".

Enter a string consisting of numbers, 26 letters, or an underscore: "^" wicked $".

Verify the user password: "^ [a-zA-Z]" w {5jc17} $"is in the correct format: begins with a letter, is between 6x18 and contains only characters, numbers, and underscores.

Verify that it contains characters such as ^% &',; =? $"": "[^% &',; =? $" x22] + ".

You can only enter Chinese characters: "^ [" u4e00-"u9fa5] {0,} $"

Verify the Email address: "^" w + ([- +.] "w +) * @" w + ([-.] "w +) *". "w + ([-.]" w +) * $".

Verify InternetURL: "^ http://(["w-]+".)+["w-]+(/["w-./?%&=]*)?$".

Verify the phone number: "^ (" d {3p4} -) | "d {3.4} -)?" d {7 XXXX- XXXXXXXX 8} $"the correct format is:" XXX-XXXXXXX "," XXXX- XXXXXXXX "," XXX-XXXXXXX "," XXX-XXXXXXXX "," XXXXXXX "and" XXXXXXXX ".

Verify the ID number (15 or 18 digits): "^" d {15} | "d {18} $".

Verify the 12 months of the year: "^ (0? [1-9] | 1 [0-2]) $" the correct format is: "01" ~ "09" and "1" ~ "12".

Verify the 31 days of the month: "^ ((0? [1-9]) | (1 | 2) [0-9]) | 30 | 31) $" the correct format is; "01" ~ "09" and "1" ~ "31".

The above is the regular expression principle and common examples shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

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

Development

Wechat

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

12
Report