Commonly used regular expressions in shell
“^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 float 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 consisting of 26 English letters
"^[A-Z]+$" //A string consisting of 26 uppercase English letters
"^[a-z]+$" //A string consisting of 26 lowercase English letters
"^[A-Za-z0-9]+$" //A string consisting of numbers and 26 English letters String
“^w+$” //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
/^((+?[0-9]{2,4}-[0-9]{3,4}-) |([0-9]{3,4}-))?([0-9]{7,8})(-[0-9]+)?$/ //Phone 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 expression matching Chinese characters: [u4e00-u9fa5]
Matching double-byte characters (including Chinese characters): [^x00-xff ]
Regular expression matching blank lines: n[s| ]*r
Regular expression 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: ^[a-zA-z]+://(\w+(-\w+ )*)(\.(\w+(-\w+)*))*(\?\S*)?$
Is the matching account legal (starting with a letter, 5-16 bytes allowed, alphanumeric underscores allowed) :^[a-zA-Z][a-zA-Z0-9_]{4,15}$
Match domestic phone numbers: (d{3}-|d{4}-)?(d{8 }|d{7})?
Matches Tencent QQ number: ^[1-9]*[1-9][0-9]*$
metacharacters and their regular expressions Behavior in expression context:
Marks the next character as a special character, or a literal character, or a backreference, or an octal escape character.
^ Matches the beginning of the input string. If the RegExp object's Multiline property is set, ^ also matches the position after 'n' or 'r'.
$ matches the end of the input string. If the RegExp object's Multiline property is set, $ also matches the position before 'n' or 'r'.
* Matches the preceding subexpression zero or more times.
+ Matches the preceding subexpression one or more times. + is equivalent to {1,}.
? Matches the preceding subexpression zero or one time. ? Equivalent to {0,1}.
{n} n is a non-negative integer that matches a certain number of n times.
{n,} n is a non-negative integer that matches at least n times.
{n,m} m and n are both non-negative integers, where n <= m. Match at least n times and at most m times. There cannot be a space between the comma and the two numbers.
? When the character immediately follows any other limiter (*, +, ?, {n}, {n,}, {n,m}), the matching pattern is non-greedy of. Non-greedy mode matches as little of the searched string as possible, while the default greedy mode matches as much of the searched string as possible.
. Matches any single character except "n". To match any character including 'n', use a pattern like '[.n]'.
(pattern) matches pattern and gets the match.
(?:pattern) matches pattern but does not obtain the matching result, which means that this is a non-acquisition match and is not stored for later use.
(?=pattern) forward lookup, matching the search string at the beginning of any string matching pattern. This is a non-fetch match, that is, the match does not need to be fetched for later use.
(?!pattern) negative lookup, opposite to (?=pattern)
x|y matches x or y.
[xyz] character set.
[^xyz] Negative value character set.
[a-z] Character range, matches any character within the specified range.
[^a-z] Negative character range, matching any character not within the specified range.
b matches a word boundary, that is, the position between a word and a space.
B matches non-word boundaries.
cx matches the control character specified by x.
d matches a numeric character. Equivalent to [0-9].
D matches a non-numeric character. Equivalent to [^0-9].
f matches a form feed. Equivalent to x0c and cL.
n matches a newline character. Equivalent to x0a and cJ.
r matches a carriage return character. Equivalent to x0d and cM.
s matches any whitespace character, including spaces, tabs, form feeds, etc. Equivalent to [fnrtv].
S matches any non-whitespace character. Equivalent to [^ fnrtv].
t matches a tab character. Equivalent to x09 and cI.
v matches a vertical tab character. Equivalent to x0b and cK.
w matches any word character including an underscore. Equivalent to '[A-Za-z0-9_]'.
W matches any non-word character. Equivalent to '[^A-Za-z0-9_]'.
xn matches n, where n is the hexadecimal escape value. The hexadecimal escape value must be exactly two digits long.
num matches num, where num is a positive integer. A reference to the match obtained.
n identifies an octal escape value or a backreference. n is a backreference if n is preceded by at least n fetched subexpressions. Otherwise, if n is an octal number (0-7), then n is an octal escape value.
nm identifies an octal escape value or a backreference. If nm is preceded by at least nm fetched subexpressions, nm is a backreference. If nm is preceded by at least n gets, then n is a backreference followed by the literal m. If neither of the previous conditions is true, and if n and m are both octal digits (0-7), nm will match the octal escape value nm.
nml If n is an octal digit (0-3), and m and l are both octal digits (0-7), then matches the octal escape value nml.
un matches n, where n is a Unicode character represented by four hexadecimal digits.
Regular expression matching Chinese characters: [u4e00-u9fa5]
Matching double-byte characters (including Chinese characters): [^x00-xff]
Regular expression matching empty lines: n[s| ]*r
Regular expression matching HTML tags: /<(.*)>.*1> |<(.*) />/
Regular expression that matches leading and trailing spaces: (^s*)|(s*$)
matches Email Regular expression of address: w+([-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
regular matching URL Expression: http://([w-]+.)+[w-]+(/[w- ./?%&=]*)?
Use regular expressions to limit the input content of the text box in the web form:
Use regular expressions to limit only Chinese input: 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 only full-width characters: onkeyup=”value=value.replace(/[^uFF00-uFFFF]/g,”)” onbeforepaste=”clipboardData.setData('text',clipboardData.getData(' text').replace(/[^uFF00-uFFFF]/g,”))”
Use regular expressions to limit the input of numbers only: onkeyup=”value=value.replace(/[ ^d]/g,”) “onbeforepaste="clipboardData.setData('text',clipboardData.getData('text').replace(/[^d]/g,"))"
Use regular expressions to limit input to numbers and English only: onkeyup="value=value.replace(/[W]/g,") "onbeforepaste="clipboardData.setData('text',clipboardData.getData('text ').replace(/[^d]/g,”))”
Commonly used regular expressions
Match Chinese Regular expression of characters: [u4e00-u9fa5]
matches double-byte characters (including Chinese characters): [^x00-xff]
matches blank lines Regular expression for: n[s| ]*r
Regular expression for matching HTML tags: /<(.*)>.*1>|<(. *) />/
Regular expression matching leading and trailing spaces: (^s*)|(s*$)
Regular expression matching IP address :/(d+).(d+).(d+).(d+)/g //
Regular expression matching email address: w+([-+.]w+)*@w+ ([-.]w+)*.w+([-.]w+)*
Regular expression matching URL: http://(/[w-]+.)+[ w-]+(/[w- ./?%&=]*)?
sql statement: ^(select|drop|delete|create|update|insert ).*$
1. Non-negative integer: ^d+$
2. Positive integer: ^[0-9]*[1-9][0-9]* $
3. Non-positive integer: ^((-d+)|(0+))$
4. Negative integer: ^-[0-9] *[1-9][0-9]*$
5. Integer: ^-?d+$
6. Non-negative floating point number: ^d+( .d+)?$
7. Positive floating point number: ^((0-9)+.[0-9]*[1-9][0-9]*)|([ 0-9]*[1-9][0-9]*.[0-9]+)| ([0-9]*[1-9][0-9]*))$
8. Non-positive floating point numbers: ^((-d+.d+)?)|(0+(.0+)?))$
9. Negative floating point numbers: ^(-((positive floating point number regular expression)))$
10, English string: ^[A-Za-z]+$
11, English uppercase string: ^[A-Z]+$
12. English lowercase string: ^[a-z]+$
13. English character and numeric string: ^[A -Za-z0-9]+$
14. Alphanumeric and underlined string: ^w+$
15. E-mail address: ^[w-] +(.[w-]+)*@[w-]+(.[w-]+)+$
16. URL: ^[a-zA-Z]+:/ /(w+(-w+)*)(.(w+(-w+)*))*(?s*)?$
or: ^http://[A-Za-z0-9]+.[ A-Za-z0-9]+[/=?%-&_~`@[]':+!]*([^& lt;>""])*$
17. Postal code: ^[1-9]d{5}$
18. Chinese: ^[u0391-uFFE5]+$
19. Telephone number :^(((d{2,3}))|(d{3}-))?((0d{2,3})|0d{2,3}-)?[1-9] d{6 ,7}(-d{1,4})?$
20. Mobile phone number: ^(((d{2,3}))|(d{3}-))? 13d{9}$
21. Double-byte characters (including Chinese characters): ^x00-xff
22. Match leading and trailing spaces: (^s*)|( s*$) (trim function like vbscript)
23. Match HTML tags: <(.*)>.*1>|<(.*) />
24. Match blank lines: n[s| ]*r
25. Extract network links in information: (h|H)(r|R)(e|E)(f |F) *= *('|”)?(w|\|/|.)+('|”| *|>)?
26. Extract the email address in the information: w+( [-+.]w+)*@w+([-.]w+)*.w+([-.]w+)*
27. Extract the picture link in the information: (s|S)(r |R)(c|C) *= *('|”)?(w|\|/|.)+('|”| *|>)?
28. Extract information IP address: (d+).(d+).(d+).(d+)
29. Extract the Chinese mobile phone number in the information: (86)*0*13d{9}
30. Extract the Chinese fixed phone number from the information: ((d{3,4})|d{3,4}-|s)?d{8}
31. Extract the Chinese phone number from the information Number (including mobile and landline): ((d{3,4})|d{3,4}-|s)?d{7,14}
32. China Post in the extracted information Encoding: [1-9]{1}(d+){5}
33. Extract floating point numbers (i.e. decimals) in the information: (-?d*).?d+
34. Extract any number in the information: (-?d*)(.d+)?
35. IP: (d+).(d+).(d+).(d+)
36. Telephone area code: /^0d{2,3}$/
37. Tencent QQ number: ^[1-9]*[1-9][0-9]*$
38. Account number (starting with a letter, allowing 5-16 bytes, allowing alphanumeric underscores): ^[a-zA-Z][a-zA-Z0-9_]{4,15}$
39, Chinese, English, numbers and underline: ^[u4e00-u9fa5_a-zA-Z0-9]+$
Thank you for reading this article, I hope it can help everyone, thank you for your support of this site !
For more detailed articles on shell regular expressions, please pay attention to the PHP Chinese website!