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

An example Analysis of the basic Grammar of regular expressions

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

Share

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

This article mainly introduces the example analysis of the basic grammar of regular expressions, which has a certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article.

1. Basic syntax of regular expressions

Two special symbols'^ 'and' $'. Their function is to indicate the beginning and end of a string, respectively. Examples are as follows:

"^ The": represents all strings starting with "The" ("There", "The cat", etc.)

"of despair$": a string that therefore ends in "of despair"

"^ abc$": a string that starts and ends with "abc"-- hehe, only "abc" itself.

"notice": represents any string that contains "notice".

Others are'*','+ 'and'?' These three symbols represent the number of repetitions of a character or a sequence of characters. They mean "no or more", "once or more" and "no or once", respectively. Here are a few examples:

"ab*": indicates that a string has an a followed by zero or more b. "a", "ab", "abbb",. )

"ab+": indicates that a string has an a followed by at least one b or more

"ab?": indicates that a string has an a followed by zero or a b

"a blank $": indicates that there are zero or one a followed by one or more b at the end of the string.

You can also use ranges, enclosed in curly braces, to indicate the range of repetitions.

"ab {2}": indicates that a string has an a followed by two b ("abb")

"ab {2,}": indicates that a string has an a followed by at least 2 b

"ab {3pr 5}": indicates that a string has an a followed by 3 to 5 b.

Please note that you must specify the lower limit of the range (for example: "{0jue 2}" instead of "{, 2}"). Also, you may have noticed,'*','+ 'and'?' Equivalent to "{0,}", "{1,}" and "{0jin1}".

There is also a "OR" operation, which means "OR":

"hi hello": indicates that there is "hi" or "hello" in a string

"(baked cd) ef": indicates "bef" or "cdef"

"(a roomb) * c": indicates a string of "a" and "b" mixed strings followed by a "c"

'.' You can replace any character:

"a. [0-9]": indicates that a string has an "a" followed by an arbitrary character and a number

"^. {3} $": represents a string of any three characters (3 characters in length)

Square brackets indicate that certain characters are allowed to appear at a specific location in a string:

"[ab]": indicates that a string has a "a" or "b" (equivalent to "a roomb")

"[a murd]": indicates that a string contains one of the lowercase'a'to'd'(equivalent to "an abcd" or "[murd]").

"^ [a-zA-Z]": represents a string that begins with a letter

"[0-9]%": a number that is preceded by a percent sign

", [a-zA-Z0-9] $": indicates that a string ends with a comma followed by a letter or number.

You can also use'^'in square brackets to indicate unwanted characters.'^ 'should be in the first place in square brackets. (for example: "% [^ a-zA-Z]%" means that letters should not appear in the two percent signs).

In order to express verbatim, you must add the transfer character'\ 'before the characters "^. $() * +? {\".

Note that no escape characters are required in square brackets.

2. Regular expression validation controls the input character type of the text box

1. You can only enter numbers and English:

two。 You can only enter numbers:

3. You can only enter full-width:

4. Those who can only enter Chinese characters:

3. Popular explanation of the application of regular expressions

/ / check whether it is made up of numbers.

/ ^ [0-9] {1pm 20} $/

^ means that the character that starts with it matches the rule immediately following ^.

$means that the character that starts with it matches the rule immediately before $.

The content in [] is an optional character set

[0-9] indicates that the required character range is between 0-9.

{1 # 20} means that the length of the numeric string is legally 1 to 20, that is, the number of occurrence of the characters in [0-9] ranges from 1 to 20.

The paired use of / ^ and $/ should indicate a rule that requires the entire string to exactly match the definition, rather than just one substring in the string.

/ / check login: you can only enter 5-20 characters that start with a letter, can be numbered, "_", "." String of

/ ^ [a-zA-Z] {1} ([a-zA-Z0-9] | [. _]) {4pm 19} $/

^ [a-zA-Z] {1} indicates that the first character is required to be a letter.

([a-zA-Z0-9] | [. _]) {4jue 19} represents a string of 4 to 9 bits in length starting from the second bit (because it follows the previous expression) and requires uppercase and lowercase letters, numbers, or a special character set [. _].

/ / verify user name: only 1-30 strings that begin with letters can be entered

/ ^ [a-zA-Z] {1pc30} $/

/ / check password: only 6-20 letters, numbers and underscores can be entered

/ ^ (\ w) {6pm 20} $/

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

/ / check ordinary telephone and fax numbers: can be "+" or start with a number, and can contain "-" and "".

/ ^ [+] {0jue 1} (\ d) {1jue 3} []? ([-]? ((\ d) | []) {1Jet 12}) + $/

\ d: used to match numbers from 0 to 9

"?" The metacharacter states that its leading object must appear zero or once in a row in the target object.

Strings that can be matched, such as: + 123-999999; + 123-999999; + 123 999999; + 123 999999, etc.

/ / verify URL

/ ^ http [s] {0http 1}:\ /\ /. + $/ or / ^ http [s] {0Magne1}:\ /. {1Magne1} $/ (indicates that the length of the url string is length ("https://") + n)"

\ /: represents the character "/".

. Represents a set of all characters

+ equals {1,}, which means 1 to positive infinity.

4. Application of regular expressions (common part)

"^\ integer $" / / non-negative integer (positive integer + 0)

"^ [0-9] * [1-9] [0-9] * $" / / positive integer

"^ ((-\ d +) | (0 +) $" / / non-positive integer (negative integer + 0)

"^-[0-9] * [1-9] [0-9] * $" / / negative integer

"^ -?\ During $" / / Integer

"^\ d + (\.\ d +)? $" / / non-negative floating point number (positive floating point number + 0)

"^ (([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *)) $" / / positive floating point number

"^ ((-\ d + (\.\ d +)?) | (0 + (\ .0 +)?) $" / / non-positive floating point number (negative floating point number + 0)

"^ (- (([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *)) $" / / negative floating point number

"^ (-?\ d +) (\.\ d +)? $" / / floating point number

"^ [A-Za-z] + $" / / A string of 26 letters

"^ [Amurz] + $" / / A string of 26 English letters in uppercase

A string of 26 lowercase letters consisting of "^ [amurz] + $" / /

"^ [A-Za-z0-9] + $" / / A string of numbers and 26 letters

A string of numbers, 26 letters, or underscores

"^ [\ w -] + (\ .[\ w -] +) * @ [\ w -] + (\ .[\ w -] +) + $" / / email address

"^ [a-zA-z] +: / / (\ w+ (-\ w+) *) (\. (\ w+ (-\ w+) *)) * (\?\ S*)? $" / / url

/ ^ (d {2} | d {4})-(0 ([1-9] {1})) | (1 [1 | 2]))-([0-2] ([1-9] {1})) | (3 [0 | 1])) $/ year-month-day

/ ^ ((0 ([1-9] {1})) | (1 [1 | 2])) / ([0-2] ([1-9] {1})) | (3 [0 | 1])) / (d {2} | d {4}) $/ month / day / year

"^ ([wmere.] +) @ ([[0-9] {1Mague 3}. [0-9] {1pr 3}. [0-9] {1pr 3}.) | (([w -] +.)) ([a-zA-Z] {2jue 4} | [0-9] {1Mague 3}) (]?) $" / / Emil

"(dwells -)? (d {4} -? d {7} | d {3} -? d {8} | ^ d {7je 8}) (- d +)? / / phone number

"^ (d {1 1dd 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1 IP 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1 recorder 2} | 1dd | 2 [0-4] d | 25 [0-5]). (d {1pm 2} | 1dd | 2 [0-4] d | 25 [0-5])

Regular expression of ^ ([0-9A-F] {2}) (- [0-9A-F] {2}) {5} $/ / MAC address

^ [- +]?\ d + (\.\ d +)? $/ / value type regular expression

Thank you for reading this article carefully. I hope the article "sample Analysis of the basic Grammar of regular expressions" shared by the editor will be helpful to you. At the same time, I also hope that you will support and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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