柚子快報激活碼778899分享:r語言 R 正則表達(dá)式
柚子快報激活碼778899分享:r語言 R 正則表達(dá)式
前言
R中的正則表達(dá)式模式有三種
1、擴(kuò)展正則表達(dá)式:默認(rèn)方式2、Perl風(fēng)格正則表達(dá)式:設(shè)置參數(shù) perl = TRUE3、字面意義正則表達(dá)式:設(shè)置參數(shù)fixed = TRUE,使用的字面意義上的正則表達(dá)式
基本字符
R中的元字符包括:
. \ | ( ) [ ] ^ $ * + ?
這些字符的含義與Python一樣
.: 表示任意字符,包括換行符\: 對字符進(jìn)行轉(zhuǎn)義|: A|B對A或B其中一個匹配,A匹配成功則不匹配B(): 字符組,括號中的模式作為一個整體進(jìn)行匹配[]: 字符集合^: 匹配字符串開頭,在[^6]表示取反,所有不是6的字符$: 匹配字符串結(jié)尾
數(shù)量詞
*: 前一個規(guī)則匹配0或無限次+: 前一個規(guī)則匹配1或無限次?: 前一個規(guī)則匹配0或1次{m}: 前一個規(guī)則匹配m次{m,n}: 前一個規(guī)則匹配m~n次,盡可能多{m,}: 前一個規(guī)則匹配m次以上,盡可能多
非貪婪模式
上述數(shù)量詞后加?,匹配到滿足規(guī)則的字符盡量少
實例
下面實例使用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: 忽略大小寫
perl: perl 語言的匹配模式
fixed: 使用的字面意義上的正則表達(dá)式
useBytes: 匹配字節(jié)
@return
匹配成功返回 TRUE,否則返回 FALSE
"""
1、符號 .
grepl('ab.', 'abc')
# out: TRUE
grepl('abc', 'Abc', ignore.case = TRUE)
# out: TRUE
grepl('ab.*', 'abcdba', fixed = T)
# out: FALSE
2、反斜杠 \
R中的反斜杠和Python中反斜杠會有所區(qū)別
R中定義的轉(zhuǎn)義字符串有:
'\n': 換行
'\r': 回車
'\t': 制表符
'\b': 退格
'\a': 響鈴
'\f': 換頁
'\v': 垂直制表符
'\\': 反斜杠
'\'': 單引號
'\"': 雙引號
'\`': 反引號
'\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)就會報錯.
要在字符常量中輸入反斜線,需要輸入兩個反斜線,即 \\
grepl('ab\\[', 'ab[]')
# out: TRUE
grepl('ab\\\\', 'ab\\')
# out: TRUE
grepl('ab\.', 'ab.')
# Error: 由"'ab\."開頭的字符串中存在'\.',但沒有這種逸出號
3、析取 |
grepl('ab|c', c('ab', 'ac', 'a'))
# out: TRUE TRUE FALSE
4、組合 ()
grepl('(a.)+c', c('ac', 'acc'))
# out: FALSE TRUE
反向引用,只能在使用小括號是才能使用
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 個小寫字母
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 的正則看起來是不是很得心應(yīng)手。
正則表達(dá)式速查表
stringr 速查表,里面也有正則表達(dá)式的內(nèi)容
柚子快報激活碼778899分享:r語言 R 正則表達(dá)式
參考文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。