当前位置:网站首页>Super complete regular expressions
Super complete regular expressions
2022-06-13 01:37:00 【Xiao Han】
List of articles
01 Regular expressions
- The role of regular expressions : The best choice for matching special characters or characters with special matching principles .
- Regular expression's greedy ( greedy ) And not greedy ( Not greed ) Pattern :
(1) Greedy mode : Regular expressions do maximum length matching , It's called greedy matching .
(2) Non greedy model : Just match the result , Less to match characters .
(3) Under legal circumstances , Regular expressions will match as many as possible . But if you want regular to match as few characters as possible , Then you can put a question mark after the symbol that represents the number?
.( for example :{n,}?
or*?
or+?
or??
or{m,n}?
) - Recommend an easy-to-use regular expression testing tool : Regular expression online test . There are also commonly used regular expressions !
Metacharacters
Regular expression languages consist of two basic character types : etymon ( normal ) Text characters and Metacharacters . Metacharacters give regular expressions processing power . Metacharacters are special characters that have special meaning in regular expressions , It can be used to specify its leading characters ( The character before the metacharacter ) The occurrence pattern in the target object .
A metacharacter is a character or group of characters that replace one or more characters . as follows :
character | describe |
---|---|
\ | Mark the next character as a special character 、 Or an original character 、 Or a Backward reference 、 Or an octal escape character . |
^ | Matches the start of the input string . |
$ | Matches the end of the input string . |
* | Match previous subexpression zero or more times . Equivalent to {0,}. |
+ | Match previous subexpression one or more times . Equivalent to {1,}. |
? | Match previous subexpression zero or once . Equivalent to {0,1}. |
{n} | n Is a non negative integer . Matched definite n Time . |
{n,} | n Is a non negative integer . Match at least n Time . |
{n,m} | m and n All non negative integers , among n <= m. Least match n Times and at most m Time . |
. | Match break (\n、\r) Any single character other than . Equivalent to [^\r\n] |
\b | Matches a word boundary , That is, the position between the word and the space . |
\B | Match non word boundaries . |
\d | Matches a numeric character . Equivalent to [0-9] . |
\D | Matches a non-numeric character . Equivalent to [^0-9] . |
\s | Matches any whitespace characters , Including Spaces 、 tabs 、 Form-feed character, etc . Equivalent to [\f\n\r\t\v] . |
\S | Matches any non-whitespace characters . Equivalent to [^ \f\n\r\t\v] . |
\w | Match the letter 、 Numbers 、 Underline . Equivalent to [A-Za-z0-9_] . |
\W | Match nonletter 、 Numbers 、 Underline . Equivalent to [^A-Za-z0-9_] . |
\f | Match a page break . Equivalent to \x0c and \cL. |
\n | Match a line break . Equivalent to \x0a and \cJ. |
\r | Match a carriage return . Equivalent to \x0d and \cM. |
\t | Match a tab . Equivalent to \x09 and \cI. |
\v | Match a vertical tab . Equivalent to \x0b and \cK. |
\xxx | Looking to 8 Hexadecimal number xxx Prescribed characters |
\xdd | Looking to 6 Hexadecimal number dd Prescribed characters |
\uxxxx | Looking to 16 Hexadecimal number xxxx Stipulated unicode character |
\ Integers | backreferences . for example :(a)\1/ matching aa;(\w)\1(\w)\2/ matching XXYY String of type |
notes : When it's time to ?
Immediately following any other qualifier (*, +, ?, {n}, {n,}, {n,m}) In the back , Matching patterns are non greedy . The non greedy pattern matches as few strings as possible , The default greedy pattern matches as many strings as possible . for example , For strings “oooo”,‘o+?’ Will match a single “o”, and ‘o+’ Will match all ‘o’.
02 Check the expression of a number
- Numbers :
^[0-9]*$
- n Digit number :
^\d{n}$
- At least n Digit number :
^\d{n,}$
- m-n Digit number :
^\d{m,n}$
- Zero and non-zero digits :
^(0|[1-9][0-9]*)$
- A number with a maximum of two decimal places beginning with a nonzero :
^([1-9][0-9]*)+(\.[0-9]{1,2})?$
- belt 1-2 Positive or negative number of decimal places :
^(\-)?\d+(\.\d{1,2})$
- Positive numbers 、 negative 、 And decimal fraction :
^(\-|\+)?\d+(\.\d+)?$
- A positive real number with two decimal places :
^[0-9]+(\.[0-9]{2})?$
- Yes 1~3 Positive real number of decimal places :
^[0-9]+(\.[0-9]{1,3})?$
- Nonzero positive integer :
^[1-9]\d*$ or ^([1-9][0-9]*){1,3}$ or ^\+?[1-9][0-9]*$
- Nonzero negative integer :
^\-[1-9][]0-9"*$ or ^-[1-9]\d*$
- Non-negative integer :
^\d+$ or ^[1-9]\d*|0$
- Non positive integer :
^-[1-9]\d*|0$ or ^((-\d+)|(0+))$
- Nonnegative floating point number :
^\d+(\.\d+)?$ or ^[1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0$
- Non positive floating point number :
^((-\d+(\.\d+)?)|(0+(\.0+)?))$ or ^(-([1-9]\d*\.\d*|0\.\d*[1-9]\d*))|0?\.0+|0$
- Positive floating point :
^[1-9]\d*\.\d*|0\.\d*[1-9]\d*$ or ^(([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 :
^-([1-9]\d*\.\d*|0\.\d*[1-9]\d*)$ or ^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$
- Floating point numbers :
^(-?\d+)(\.\d+)?$ or ^-?([1-9]\d*\.\d*|0\.\d*[1-9]\d*|0?\.0+|0)$
03 Expression for check character
- Chinese characters :
^[\u4e00-\u9fa5]{0,}$
- English and numbers :
^[A-Za-z0-9]+$ or ^[A-Za-z0-9]{4,40}$
- The length is 3-20 All characters of :
^.{3,20}$
- from 26 A string of English letters :
^[A-Za-z]+$
- from 26 A string of uppercase letters :
^[A-Z]+$
- from 26 A string of lowercase letters :
^[a-z]+$
- By numbers and 26 A string of English letters :
^[A-Za-z0-9]+$
- By digital 、26 A string of English letters or underscores :
^\w+$
or^\w{3,20}$
- chinese 、 english 、 Numbers include underscores :
^[\u4E00-\u9FA5A-Za-z0-9_]+$
- chinese 、 english 、 Number but excluding symbols such as underscores :
^[\u4E00-\u9FA5A-Za-z0-9]+$
or^[\u4E00-\u9FA5A-Za-z0-9]- {2,20}$
- Can be entered with
^%&',;=?$\"
Equal character :[^%&',;=?$\x22]+
- Disable input containing ~ The characters of :
[^~\x22]+
04 Expression of special requirements
- Email Address :
^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$
- domain name :
[a-zA-Z0-9][-a-zA-Z0-9]{0,62}(\.[a-zA-Z0-9][-a-zA-Z0-9]{0,62})+\.?
- InternetURL:
[a-zA-z]+://[^\s]* or ^http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$
- Phone number :
^(13[0-9]|14[5|7]|15[0|1|2|3|4|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$
- Phone number (“XXX-XXXXXXX”、“XXXX-XXXXXXXX”、“XXX-XXXXXXX”、“XXX-XXXXXXXX”、"XXXXXXX" and "XXXXXXXX):
^(\(\d{3,4}-)|\d{3.4}-)?\d{7,8}$
- Domestic phone number (0511-4405222、021-87888822):
\d{3}-\d{8}|\d{4}-\d{7}
- Phone number regular expression ( Mobile number support ,3-4 Bit area code ,7-8 Bit live number ,1-4 Extension number ):
((\d{11})|^((\d{7,8})|(\d{4}|\d{3})-(\d{7,8})|(\d{4}|\d{3})-(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1})|(\d{7,8})-(\d{4}|\d{3}|\d{2}|\d{1}))$)
- ID number (15 position 、18 Digit number ), The last bit is the check bit , May be a number or character X:
(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)
- Is the account number legal ( Beginning of letter , allow 5-16 byte , Allow alphanumeric underscores ):
^[a-zA-Z][a-zA-Z0-9_]{4,15}$
- password ( Start with a letter , The length is in 6~18 Between , Can only contain letters 、 Numbers and underscores ):
^[a-zA-Z]\w{5,17}$
- Strong password ( Must contain a combination of upper and lower case letters and numbers , Special characters cannot be used , The length is in 8-10 Between ):
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{8,10}$
- Strong password ( Must contain a combination of upper and lower case letters and numbers , Special characters can be used , The length is in 8-10 Between ):
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,10}$
- Date format :
^\d{4}-\d{1,2}-\d{1,2}
- One year 12 Months (01~09 and 1~12):
^(0?[1-9]|1[0-2])$
- A month 31 God (01~09 and 1~31):
^((0?[1-9])|((1|2)[0-9])|30|31)$
- Input format of money :
- There are four forms of money we can accept :“10000.00” and “10,000.00”, And no “ branch ” Of “10000” and “10,000”:
^[1-9][0-9]*$
- This means that either of them can't 0 Number at the beginning , however , It also means a character "0" Not through , So we take the following form :
^(0|[1-9][0-9]*)$
- One 0 Or one doesn't 0 Number at the beginning . We can also allow a minus sign at the beginning :
^(0|-?[1-9][0-9]*)$
- This means a 0 Or a start that may be negative is not 0 The number of . Let users 0 The beginning is good. . Remove the minus sign as well , Because money can't be negative . What we're going to add here is the possible decimal part :
^[0-9]+(.[0-9]+)?$
- It must be noted that , There should be at least one after the decimal point 1 digit , therefore "10." No way , however “10” and “10.2” Yes. :
^[0-9]+(.[0-9]{2})?$
- In this way, we stipulate that there must be two decimal places after the decimal point , If you think it's too harsh , It can be like this :
^[0-9]+(.[0-9]{1,2})?$
- This allows the user to write only one decimal place . Now it's time to think about commas in numbers , We can do this :
^[0-9]{1,3}(,[0-9]{3})*(.[0-9]{1,2})?$
- 1 To 3 A digital , Follow any one comma +3 A digital , Comma as optional , Not necessarily :
^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(.[0-9]{1,2})?$
- xml file :
^([a-zA-Z]+-?)+[a-zA-Z0-9]+\\.[x|X][m|M][l|L]$
- Regular expression of Chinese characters :
[\u4e00-\u9fa5]
- Double byte character :
[^\x00-\xff]
( Including Chinese characters , Can be used to calculate the length of a string ( A double byte character length meter 2,ASCII Character meter 1)) - Regular expression for blank lines :
\n\s*\r
( Can be used to delete blank lines ) - HTML Tagged regular expression :
<(\S*?)[^>]*>.*?|<.*? />
( Regular expression of first and last whitespace characters :^\s*|\s*$
or(^\s*)|(\s*$)
( Can be used to delete blank characters at the beginning and end of a line ( Including Spaces 、 tabs 、 Page breaks and so on ), Very useful expressions ) - China Post Code :
[1-9]\d{5}(?!\d)
( China Post code is 6 Digit number ) - IPv4 Address :
((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})(\.((2(5[0-5]|[0-4]\d))|[0-1]?\d{1,2})){3}
边栏推荐
- [projet cs144 de Stanford Computing Network] lab1: Stream reassembler
- Logical operation bit operation
- How to print infinite symbol in WPS
- [wsl2]wsl2 migrate virtual disk file ext4 vhdx
- [wsl2] restrict wsl2 accessible hardware resources (cpu/ memory)
- [andoid][step pit]cts 11_ Testbootclasspathandsystemserverclasspath at the beginning of R3_ Analysis of nonduplicateclasses fail
- A summary of global variables and typedef
- [MathType] use MathType to output latex style formula
- Rasa dialogue robot helpdesk (III)
- ES6 deconstruction assignment
猜你喜欢
随机推荐
D template instance does not match declaration
【软考】软件设计师知识点整理(待更新)
[official document summary] writing standards for academic dissertations of National University of science and technology
[leetcode] valid phone number Bash
【斯坦福计网CS144项目】Lab1: StreamReassembler
leetcode. 349. intersection of two arrays
Camera model_
项目实训(十七)---个人工作总结
[从零开始学习FPGA编程-21]:进阶篇 - 架构 - VerilogHDL编码规范
Binary tree traversal - recursive and iterative templates
Design of distributed game server
Tweets movement description and chart display
ES6解构赋值
QT color extraction
How to turn on the hotspot for the mobile phone after the computer is connected to the network cable
About constructive code blocks, static code blocks, and constructor execution order
Machine learning basic SVM (support vector machine)
Startup, connection and stop of MySQL service
Leetcode question brushing 07 double pointer
谷歌的智能出价有几种?