R language--Regular expression

Source: Internet
Author: User

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
    1. Strong flexibility, logic and functionality
    2. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.