Defined
A regular expression is a logical formula for manipulating a string.
Action Object
The regular expression is the object of the text .
Role
* Logic Filter
* Precise gripping
Characteristics
- Strong flexibility, logic and functionality
- Complex control of strings can be achieved quickly and in a very simple way
Grammar rules
\ escape Character
. Any character other than line break
^ Put at the beginning of the sentence, indicating the start of a line of string
$ put at the end of a sentence, indicating the ending of a line of strings
* 0 or more previous characters
+ one or more previous characters
? 0 or one of the preceding characters
square brackets [], which represent any one of the characters that can be matched. and ^ in [] stands for "non",-represents "between"
Any one of the characters in the –[qjk]:q,j,k
–[^QJK]: Any other characters that are not q,j,k
Any lowercase character in –[a-z]:a to Z
–[^A-Z]: Other characters that are not any one-to-Z lowercase characters (can be uppercase characters)
–[A-ZA-Z]: any one English letter
–[a-z]+: One or more lowercase English letters
| Or
Parentheses () with curly braces {} with "|" Use
Special Note: reserved characters require the escape character \ to escape the representation
For example:
common meaning of special escape characters
? \ n: Line break
? \t:tab
? \w: Any letter (including underscores) or numbers [a-za-z0-9_]
? \w:\w's antisense meaning is [^a-za-z0-9_]
? \d: Any number that is [0-9]
? \d:\d's antisense meaning is [^0-9]
? \s: Any space, such as space, tab, newline, etc.
? \s:\s, any non-whitespace
Common functions of regular expressions
? GREPL: Returns a logical value
? grep: Returns the matching ID,
? Agrep: Returns the matching ID,
? Regular Replacement: Sub and Gsub
The difference between the two is as follows
# Replace B with BGsub (pattern ="B", replacement ="B", x ="Baby")[1]"BaBy"Gsub (pattern ="B", replacement ="B", x = C ("ABCB","Boy.","Baby"))[1]"ABCB" "Boy." "BaBy"# Replace only the first BSub (pattern ="B", replacement ="B", x ="Baby")[1]"Baby"Sub (pattern ="B", replacement ="B", x = C ("ABCB","Baby"))[1]"ABCB" "Baby"
? REGEXPR: Returns a number, 1 for the match, 1 for the mismatch, and two for the length of the match and whether to use Usebytes
? Regexec: Returns a list, the first match in the string and its length, and whether to use Usebytes
? GREGEXPR: Returns a list, each match and its length, and whether to use Usebytes
R language--Regular expression