Regular match and replace the regexp & amp; Regsub
Regular match is to use a method of the regular expression matching string; In the process of script writing, often need to deal with some text, and this may be only part of the text information is useful, we need to extract the useful information from the text. At that time, you need to write the specific format of the regular expression, fetching out will conform to the regular expressions in the text of the string, and then carries on the decomposition, combination, replace such as processing, get in line with the requirements of processing result,
Regular expression is very flexible, which make it has strong matching ability, skilled writing regular expressions, can match almost any form of string, therefore, in the script design, skillfully using regular expressions, is a very important skill, can effectively improve the running efficiency of the script,
Need to use regular expressions, with the aid of two important tools: the regexp and regsub, used to match and replace respectively,
(note: the regexp & amp; Regsub action object is character text, if you need to deal with file directly, you can use the sed & amp; Awk)
Sed & amp; Awk usage reference this post: https://www.cnblogs.com/xiaoxie2014/p/9883168.html
The regexp
All or part of the regexp is used to determine whether the regular expression match the target string command, matching returns 1, otherwise it returns 0,
Regexp has two kinds of usage, one is only match, the other one is the matching substring
The first use, for example the regexp {^ ([0-9] + [a-z] + | [a-z] + [0-9] +) $} 123 ABC, the regular expression to match "digital beginning and lowercase letters end" or "capital letter and end digital" expression, so the return value is 1
Explained in detail: ^ match beginning, $matches the end, in the middle of the | said, "or" + characters appear in front of one or more
With a way to string matching for example the second regexp {([0-9] +) \ s ([a-z] +)} "there are 100 apples" str01 sub01 sub02, the regular expression to match "add a space to add one or more of the figures one or more of the lowercase letter", return 1
Explained in detail: expression includes two substring ([0-9] +) and (+) [a-z], so str01="100 apples" substring sub01=100 substring sub02=apples
Regsub
Regsub is used to meet part of the regular expression in the target string replacement, and results in the new variable, after will replace match returns 1, otherwise it returns 0,
For example regsub {there} "live there mattress" their x, a regular expression is there, match in the string word there, and replace them with their, and will replace the string of deposit in variable x, return 1, so the value of the variable x is $x="live their mattress"
Note: a options - all regsub, if don't have the open this option, only replace the first match, or replace all matched to the target
From https://www.cnblogs.com/xiaoxie2014/p/9347620.html