前言
R中的正則表達(dá)式模式有三種
- 1、擴(kuò)展正則表達(dá)式:默認(rèn)方式
- 2、
Perl風(fēng)格正則表達(dá)式:設(shè)置參數(shù)perl = TRUE - 3、字面意義正則表達(dá)式:設(shè)置參數(shù)
fixed = TRUE,使用的字面意義上的正則表達(dá)式
基本字符
R中的元字符包括:
. \ | ( ) [ ] ^ $ * + ?
這些字符的含義與Python一樣
-
.: 表示任意字符,包括換行符 -
\: 對(duì)字符進(jìn)行轉(zhuǎn)義 -
|:A|B對(duì)A或B其中一個(gè)匹配,A匹配成功則不匹配B -
(): 字符組,括號(hào)中的模式作為一個(gè)整體進(jìn)行匹配 -
[]: 字符集合 -
^: 匹配字符串開(kāi)頭,在[^6]表示取反,所有不是6的字符 -
$: 匹配字符串結(jié)尾
數(shù)量詞
-
*: 前一個(gè)規(guī)則匹配0或無(wú)限次 -
+: 前一個(gè)規(guī)則匹配1或無(wú)限次 -
?: 前一個(gè)規(guī)則匹配0或1次 -
{m}: 前一個(gè)規(guī)則匹配m次 -
{m,n}: 前一個(gè)規(guī)則匹配m~n次,盡可能多 -
{m,}: 前一個(gè)規(guī)則匹配m次以上,盡可能多
非貪婪模式
- 上述數(shù)量詞后加
?,匹配到滿足規(guī)則的字符盡量少
實(shí)例
下面實(shí)例使用grepl函數(shù)
grepl <- function (pattern, x, ignore.case = FALSE, perl = FALSE, fixed = FALSE,
useBytes = FALSE)
{
if (!is.character(x))
x <- as.character(x)
.Internal(grepl(as.character(pattern), x, ignore.case, FALSE,
perl, fixed, useBytes, FALSE))
}
"""
@argument:
pattern: 匹配規(guī)則
x: 需要去匹配的字符串
ignore.case: 忽略大小寫(xiě)
perl: perl 語(yǔ)言的匹配模式
fixed: 使用的字面意義上的正則表達(dá)式
useBytes: 匹配字節(jié)
@return
匹配成功返回 TRUE,否則返回 FALSE
"""
1、符號(hào) .
grepl('ab.', 'abc')
# out: TRUE
grepl('abc', 'Abc', ignore.case = TRUE)
# out: TRUE
grepl('ab.*', 'abcdba', fixed = T)
# out: FALSE
2、反斜杠 \
R中的反斜杠和Python中反斜杠會(huì)有所區(qū)別
R中定義的轉(zhuǎn)義字符串有:
'\n': 換行
'\r': 回車(chē)
'\t': 制表符
'\b': 退格
'\a': 響鈴
'\f': 換頁(yè)
'\v': 垂直制表符
'\\': 反斜杠
'\'': 單引號(hào)
'\"': 雙引號(hào)
'\`': 反引號(hào)
'\nnn': 八進(jìn)制字符(1~3位置的n為八進(jìn)制數(shù)字)
'\xnn': 十六進(jìn)制字符(1~2位置的n為十六進(jìn)制數(shù)字)
'\unnnn': 十六進(jìn)制字符(1~4)
'\Unnnnnnnn': 十六進(jìn)制字符(1~8)
如果反斜線后的轉(zhuǎn)義字符串不在上表范圍內(nèi),系統(tǒng)就會(huì)報(bào)錯(cuò).
要在字符常量中輸入反斜線,需要輸入兩個(gè)反斜線,即 \\
grepl('ab\\[', 'ab[]')
# out: TRUE
grepl('ab\\\\', 'ab\\')
# out: TRUE
grepl('ab\.', 'ab.')
# Error: 由"'ab\."開(kāi)頭的字符串中存在'\.',但沒(méi)有這種逸出號(hào)
3、析取 |
grepl('ab|c', c('ab', 'ac', 'a'))
# out: TRUE TRUE FALSE
4、組合 ()
grepl('(a.)+c', c('ac', 'acc'))
# out: FALSE TRUE
反向引用,只能在使用小括號(hào)是才能使用
grepl('c(..) s\\1', c('cat sat', 'cat saa'))
# out: TRUE FALSE
5、字符集合 []
grepl('[Tt]he', c('the', 'The'))
# out: TRUE TRUE
# 范圍:a-z 表示 26 個(gè)小寫(xiě)字母
grepl('[a-z]he', c('the', 'she', 'The'))
# out: TRUE TRUE FALSE
# 取反,所有不是 t 的字符
grepl('[^t]he', c('the', 'she', 'The'))
# out: FALSE TRUE TRUE
# 特殊字符失去特殊含義,. 不再表示任意字符
grepl('[.]he', c('the', '.he'))
# out: FALSE TRUE
6、邊界 ^ $
grepl('[Tt]he', c('the', 'The', 'other'))
# out: TRUE TRUE TRUE
grepl('^[Tt]he', c('the', 'The', 'other'))
# out: TRUE TRUE FALSE
grepl('[Tt]he$', c('the', 'The', 'other'))
# out: TRUE TRUE FALSE
7、數(shù)量詞 * + ? {}
grepl('.*', 'abcdba')
# out: TRUE
grepl('.*', '')
# out: TRUE
grepl('.+', 'abcdba')
# out: TRUE
grepl('.+', '')
# out: FALSE
grepl('ab?', 'a')
# out: TRUE
grepl('ab?', 'ab')
# out: TRUE
grepl('ab{3}', 'abbb')
# out: TRUE
grepl('ab{3}', 'abb')
# out: FALSE
grepl('ab{3,5}', 'abbbb')
# out: TRUE
grepl('ab{3, }', 'abbbb
grepl('ab{3, }', 'abbbb')
grepl('ab{3,}', 'abbbb')
grepl('ab{,4}', 'abbbb')
# out: TRUE
非貪婪模式
sub<-function (pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)
{
if (!is.character(x))
x <- as.character(x)
.Internal(sub(as.character(pattern), as.character(replacement),
x, ignore.case, perl, fixed, useBytes))
}
"""
sub 函數(shù) 與 grepl 函數(shù)參數(shù)基本一致
將匹配到的字符串替換為 replacement 參數(shù)指定的數(shù)據(jù)
"""
sub('ab{3,8}', '', 'abbbbbbbc')
# out: 'c'
sub('ab{3,8}?', '', 'abbbbbbbc')
# out: 'bbbbc'
有了 Python 正則的鋪墊,R 的正則看起來(lái)是不是很得心應(yīng)手。
正則表達(dá)式速查表

image.png
stringr 速查表,里面也有正則表達(dá)式的內(nèi)容

image.png

image.png