Regular expression formula regular expression learning tool

Time:2021-9-2

Too many introductions signed on the Internet are fierce essays, which can be understood while watching, but can’t help memory. Inspired by the five stroke radical table formula “look at the first three or two catties…”,  
Try to write “regular expression mnemonic formula”, also known as “regular doggerel”, version 0.1, absolutely original, counterfeiting must be investigated,:)  
Note: This article is only for the convenience of memory when learning regularization, and can not replace the systematic and comprehensive learning process. Please correct the mistakes and omissions!  
In fact, regular is also snobbish, cutting his head to put money in;  ( Refers to the start symbol ^ and the end symbol $)  
If you can’t recognize the special symbol, get a reverse bar to guide the way;  ( Means \  \* And other special symbols)  
The back bar is followed by a small W,   Represented by numbers and letters;  (\ W followed by numbers and letters\ D (followed by number)  
The reverse bar is followed by a small D,   Only numbers;  
The reverse bar is followed by a small a,   Alarm symbol beeps;  
The reverse bar is followed by a small B,   Word boundary or backspace;  
The back bar is followed by a small t,   The tabulation symbols are clear;  
The reverse bar is followed by a small R,   I know the enter symbol;  
The reverse bar is followed by a small s,   Space symbols are important;  
Lowercase and uppercase are really too much;  
The reverse bar is followed by a big W,   Alphanumeric stand aside;  
The reverse bar is followed by a big s,   Blank also stands aside;  
The reverse bar is followed by a big D,   The number stands aside;  
Inverted frame followed by big B,   Excluding the beginning and end;  

A single character to repeat, three symbols to help;  (* + ?) 
0   Star plus 1   To infinity, the question mark is only 0   And 1;  (* Table 0-n+ Table 1-N;? Table 0-1 (repeated)  
Curly braces have more knowledge and strong ability of repeated operation;  ({ n}  { n,}  { n,m})  
To repeat a string, enclose it;  (( abc){3}   Indicates that the string “ABC” is repeated 3 times  ) 
Special collection customization, brackets to help you;  
Escape symbols don’t work. Queue up one by one;  
There are too many rows. Please do me a favor;  ([ 1-5])  
If the tip is put into square brackets, the antisense definition is powerful;  ([^ a] Any character other than “a”  ) 
1. The vertical action is not small, and the two sides are regular and replace each other;  ( (the same key as “\” on the keyboard)  
1. The vertical can be used many times, and the complex definition is very convenient;  
Round brackets, multi-purpose;  
Reverse reference the specified group, and the number typesetting character corresponds to it;  (“\ The number “1” in B (\ W +) \ B \ S + \ 1 \ B “refers to the preceding” (\ W +) “)  
Support group name customization, with question marks and angle brackets;  (“(?< “\ W +” is defined as a group in word > \ W +) “(the group name is” word “)  
Round brackets are used for many purposes, and the position designation depends on it;  
Question mark and equal sign string, positioned in front of the string;  (“\ b\w+(?= (ing \ b) “locate the string before” ing “)  
To locate the string, insert a less than sign in the middle;  (“(?<=\ Bsub) \ W + \ B “locate the string after” sub “)  
Question mark plus exclamation mark followed by string;  
PHPer knows,  ! It means to take the opposite;  
Not followed by this string, all in line to report for duty;  (“\ w*d(?! (OG) \ w * “,” dog “does not comply,” do “complies)  
The question mark is less than the exclamation mark, followed by a string;  
Do not put this string in front, all in line to report for duty;  
The dot and asterisk are greedy. Adding a question mark is not greedy;  
The plus sign and question mark are guaranteed, and they shall be repeated at least once;  
Two question marks, the old rule, 0 times and 1 time;  
Curly braces followed by a?, Greed becomes non greed;  
There are still many that can’t fit, waiting to be added in the future.  
——————————————————————————– 

Some common regular expressions  

“^ \ D + $” / / 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  
“^-?\ D + $”/ / 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 English letters  
“^ [A-Z] + $” / / a string composed of 26 uppercase letters  
“^ [A-Z] + $” / / a string composed of 26 lowercase letters  
“^ [a-za-z0-9] + $” / / a string composed of numbers and 26 English letters  
“^ \ W + $” / / a string consisting of numbers, 26 English 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  
“^([w-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$” //Emil 
“(d+-)?( d{4}-? d{7}|d{3}-? d{8}|^d{7,8})(-d+)?”  // Telephone number  
“^(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5]).(d{1,2}|1dd|2[0-4]d|25[0-5])$”  // IP address  

Regular expressions matching Chinese characters:  [\ u4e00-\u9fa5]  
Matching double byte characters (including Chinese characters): [^ \ X00 – \ XFF]  
Regular expression matching empty lines: \ n [\ s|  ]*\ r  
Regular expressions matching HTML Tags: / < (. *) >. * < \ / \ 1 > | < (. *)  \/>/ 
Regular expression matching leading and trailing spaces: (^ \ s *) | (\ s * $)  
Regular expression matching email address: \ W + ([- +.] \ W +) * @ \ W + ([-.] \ W +) * \. \ W + ([-.] \ W +)*  
Regular expression matching URL of web address: ^ [a-za-z] +: / / (\ \ W + (- \ \ W +) *) (\ \. (\ \ W + (- \ \ W +) *) * (\ \\\ S*)?$  
Whether the matching account number is legal (starting with a letter, 5-16 bytes are allowed, and alphanumeric underscores are allowed): ^ [a-za-z] [a-za-z0-9_]{ 4,15}$  
Matching domestic telephone number: (\ D {3} – | \ D {4} -)\ d{8}|\d{7})?  
Matching Tencent QQ No.: ^ [1-9] * [1-9] [0-9]*$  

The following table is a complete list of metacharacters and their behavior in the context of regular expressions:  

\   Marks the next character as a special character, or a literal character, or a backward reference, or an octal escape character.  

^   Matches the start of the input string. If set   RegExp   Multiline of object   Properties^   Also match  ‘\ n’   or  ‘\ r’   Position after.  

$   Matches the end of the input string. If set   RegExp   Multiline of object   Properties$   Also match  ‘\ n’   or  ‘\ r’   Previous position.  

*   Matches the previous subexpression zero or more times.  

+   Matches the previous subexpression one or more times+   Equivalent to  { 1,}。  

?   Matches the previous subexpression zero or once.?   Equivalent to  { 0,1}。  

{n}   n   Is a nonnegative integer that matches the determined n   Times.  

{n,}   n   Is a nonnegative integer that matches at least n   Times.  

{n,m}   m   and   n   Are non negative integers, where n  <=  m。 Least match   n   Times and matches at most   m   Times. There cannot be a space between a comma and two numbers.  

?   When this character follows any other qualifier  (*, +, ?, { n},  { n,},  { n,m})   Later, the matching pattern is non greedy. The non greedy pattern matches as few strings as possible, while the default greedy pattern matches as many strings as possible.  

.   Matching Division  “\ n”   Any single character other than. To match include  ‘\ n’   For any characters, please use the image  ‘[.\ n]’   Mode.  
(pattern)   Match pattern   And get this match.  

(?: pattern)   Match pattern   However, the matching result is not obtained, that is, it is a non obtained match and will not be stored for future use.  

(?= pattern)   Forward pre check, in any match   pattern   Matches the lookup string at the beginning of the string. This is a non fetched match, that is, the match does not need to be fetched for later use.  

(?! pattern)   Negative pre check, and (?)= Pattern has the opposite effect  

x|y   matching   x   or   y。  

[xyz]   Character set.  

[^xyz]   Negative character set.  

[a-z]   Character range that matches any character within the specified range.  

[^a-z]   A negative character range that matches any character that is not within the specified range.  

\b   Match a word boundary, that is, the position between the word and the space.  

\B   Matches non word boundaries.  

\cx   Matches the control character indicated by X.  

\d   Matches a numeric character. Equivalent to  [ 0-9]。  

\D   Matches a non numeric character. Equivalent to  [^ 0-9]。  

\f   Match a page feed. Equivalent to  \ x0c   and  \ cL。  

\n   Match a newline character. Equivalent to  \ x0a   and  \ cJ。  

\r   Match a carriage return. Equivalent to  \ x0d   and  \ cM。  

\s   Matches any white space characters, including spaces, tabs, page breaks, and so on. Equivalent to[  \ f\n\r\t\v]。  

\S   Matches any non whitespace characters. Equivalent to  [^ \ f\n\r\t\v]。  

\t   Match a tab. Equivalent to  \ x09   and  \ cI。  

\v   Match a vertical tab. Equivalent to  \ x0b   and  \ cK。  

\w   Matches any word characters that include underscores. Equivalent to ‘[a-za-z0-u9]’.  

\W   Matches any non word characters. Equivalent to  ‘[^ A-Za-z0-9_]’。  

\xn   matching   n. Among them   n   Is a hexadecimal escape value. Hexadecimal escape value must be two digits long.  

\num   matching   Num, where num is a positive integer. A reference to the match obtained.  

\n   Identifies an octal escape value or a backward reference. If  \ n   Before at least   n   Gets a subexpression, then   n   Is a backward reference. Otherwise, if   n   Is an octal digit  ( 0-7), then   n   Is an octal escape value.  

\nm   Identifies an octal escape value or a backward reference. If  \ nm   At least one is before   preceded   by   at   least   nm   Gets a subexpression, then   nm   Is a backward reference. If  \ nm   At least before   n   Gets, then   n   Is a text followed by   m   Backward reference to. If none of the above conditions are met, if   n   and   m   All octal digits  ( 0-7), then  \ nm   Will match octal escape values   nm。  

\nml   If   n   Is an octal digit  ( 0-3), and   m   and   l   All octal digits  ( 0-7), the octal escape value is matched   nml。  

\un   matching   n. Among them   n   Is a Unicode character represented by four hexadecimal digits.  

Regular expressions matching Chinese characters:  [ u4e00-u9fa5]  

Matching double byte characters (including Chinese characters): [^ X00 XFF]  

Application: calculate the length of string (a double byte character length meter 2, ASCII character meter 1)  

String.prototype.len=function(){return this.replace([^x00-xff]/g,”aa”).length;} 

Regular expressions that match empty lines: n[s|  ]* r  

Regular expressions matching HTML Tags: / < (. *) >. * < / 1 > | < (. *)  />/ 

Regular expression matching leading and trailing spaces: (^ s *) | (s * $)  

Application: there is no trim function like VBScript in JavaScript, so we can use this expression to implement it, as follows:  

String.prototype.trim = function() 

return this.replace(/(^s*)|(s*$)/g, “”); 

Decompose and convert IP addresses using regular expressions:  

The following is a JavaScript program that uses regular expressions to match IP addresses and convert IP addresses into corresponding values:  

function IP2V(ip) 

re=/(d+).(d+).(d+).(d+)/g  // Regular expression matching IP address  
if(re.test(ip)) 

return RegExp.$1*Math.pow(255,3))+RegExp.$2*Math.pow(255,2))+RegExp.$3*255+RegExp.$4*1 

else 

throw new Error(“Not a valid IP address!”) 

However, if the above program does not use regular expressions, it may be simpler to decompose it directly with split function. The program is as follows:  

var ip=”10.100.20.168″ 
ip=ip.split(“.”) 
Alert (“IP value is:” + (IP [0] * 255 * 255 * 255 + IP [1] * 255 * 255 + IP [2] * 255 + IP [3] * 1))  

Regular expression matching email address: W + ([- +.] W +) * @ w + ([-.] W +) *. W + ([-.] W +)*  

Regular expression matching URL of web address: http: / / ([w -] +) + [w -] + (/ [w-  ./?%&=]*)? 

Algorithm program for removing repeated characters in string by regular expression:  

var s=”abacabefgeeii” 
var s1=s.replace(/(.).*1/g,”$1″) 
var re=new RegExp(“[“+s1+”]”,”g”) 
var s2=s.replace(re,””) 
alert(s1+s2)  // The result is: abcefgi  

A JavaScript program that uses regular expressions to extract the file name from the URL address. The following result is page1  

s=”http://www.9499.net/page1.htm” 
s=s.replace(/(.*/){0,}([^.]+).*/ig,”$2″) 
alert(s) 

Use regular expressions to restrict the text box input in the web form:  

Use regular expression to restrict the input of Chinese: onkeyup = “value = value. Replace (/ [^ u4e00-u9fa5] / g, ”)”   onbeforepaste=”clipboardData.setData(‘text’,clipboardData.getData(‘text’).replace(/[^u4E00-u9FA5]/g,”))”  

Use regular expressions to limit the input of full width characters:   onkeyup=”value=value.replace(/[^uFF00-uFFFF]/g,”)”   onbeforepaste=”clipboardData.setData(‘text’,clipboardData.getData(‘text’).replace(/[^uFF00-uFFFF]/g,”))”  

Use regular expression to restrict the input of only numbers: onkeyup = “value = value. Replace (/ [^ D] / g, ”)  ” onbeforepaste=”clipboardData.setData(‘text’,clipboardData.getData(‘text’).replace(/[^d]/g,”))”  

Use regular expressions to restrict the input of numbers and English: onkeyup = “value = value. Replace (/ [w] / g, ”)  ” onbeforepaste=”clipboardData.setData(‘text’,clipboardData.getData(‘text’).replace(/[^d]/g,”))”
Regular expression formula regular expression learning tool
Regular expression formula regular expression learning toolregexp.zip
Regular expression formula regular expression learning tool
Regular expression formula regular expression learning toolRexlist + regex processing tool + V2 [1]. 0 + Green official version. Rar
Regular expression formula regular expression learning toolRegex test tool + 1 [1]. 0 + regextester.rar
Regular expression formula regular expression learning tool
Regular expression formula regular expression learning toolRegex Tool + mtracer + 2 [1]. 0.rar