The shell can also use regular group captures, but you cannot capture groupings by using a form such as $ or \1, which can be obtained through array ${bash_rematch}, such as ${bash_rematch[1]},${bash_rematch[n]}
Take ip= "121.0.2.2" for example, the shell script code is as follows (of course, you want to make a more general interactive script that can be implemented by expect):
#!/bin/bash
ip= "121.0.2.2"
if [[$ip =~ ^ ([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \. ( [0-9] {1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \. ([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) \. ([0-9]{1,2}|1[0-9][0-9]|2[0-4][0-9]|25[0-5]) $]]
Then
echo "Match"
Echo ${bash_rematch[1]}
Echo ${bash_rematch[2]}
Echo ${bash_rematch[3]}
Echo ${bash_rematch[4]}
Else
echo "Not match"
Fi
The shell script matches the email in the file and logs it to the file.
#! /bin/bash
function Read_file () {
For line in ' cat $ '
Todo
If [' Echo $line |grep ' ^[a-za-z0-9_-]*@[a-za-z_-]*\.[ a-za-z_-]*$ "'];then
echo $line >> Result.txt
Else
echo "---" >> result.txt
Fi
Done
}
#eg
Read_file A.txt
Common shell Regular Expressions
^user$ matches rows that contain only USER
[tty]$ matches rows with TTY endings
\. Match a line with a period
^d.. X.. X.. x matches the directory where users, user groups, and other user group members have executable permissions
^[^L] matches the list of directories that exclude the associated directory
[. *0] matches any character before or after 0
[^$] matches a blank line
[^.*$] matches any string in a matching row
^......$ matches a line that includes 6 characters
[A-za-z] matches any single character
[A-z] [a-z]* matches at least one lowercase letter
[^0-9\$] matches a non-numeric or dollar ID
[^0-0a-za-z] matches non-digits or letters
De.. Ce matches the first two letters to DE, followed by two arbitrary characters, and finally CE
[1..9] Match 1 to 9
^.$ matches a line with only one character
^\. [0-9] [0-9] matches a line starting with a period and two digits
' Device ' matches the word Device
De[vv]ice\. Match word device or device
[0-9]\{2\}-[0-9]\{2\}-[0-9]\{4\} Match date format dd-mm-yyyy
[^.*$] matches any row
^[1-9]\d{5}$ Matching ZIP code
((\+? [0-9] {2,4}\-[0-9]{3,4}\-) | ([0-9]{3,4}\-)]? ([0-9]{7,8}) (\-[0-9]+)? $/matching phone number
((\ (\d{2,3}\)) | (\d{3}\-))? 13\d{9}$ Matching cell 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]) $ "Match IP address
[\u4e00-\u9fa5] A regular expression that matches Chinese characters
[^\x00-\xff] matches two-byte characters (including Chinese characters)
\n[\s|] *\r regular expressions that match empty rows
/< (. *) >.*<\/\1>|< (. *) \/>/regular expressions that match HTML tags
(^\s*) | (\s*$) A regular expression that matches a trailing space
\w+ ([-+.] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) * Matching the regular expression of the email address
^[a-za-z]+://(\\w+ (-\\w+) *) (\ \\w+ (-\\w+) *) * (\\?\\s*)? $ regular Expression matching URL URLs
^[a-za-z][a-za-z0-9_]{4,15}$ Match account number is valid (start letter, allow 5-16 bytes, allow alphanumeric underline)
(\d{3}-|\d{4}-)? (\d{8}|\d{7})? Match domestic phone number
^[1-9]*[1-9][0-9]*$ matching Tencent QQ number