R gsub Function

gsub() function replaces all matches of a string, if the parameter is a string vector, returns a string vector of the same length and with the same attributes (after possible coercion to character). Elements of string vectors which are not substituted will be returned unchanged (including any declared encoding).

gsub(pattern, replacement, x, ignore.case = FALSE, perl = FALSE,
fixed = FALSE, useBytes = FALSE)

• pattern: string to be matched
• replacement: string for replacement
• x: string or string vector
• ignore.case: if TRUE, ignore case
...

> x <- "R Tutorial"
> gsub("ut","ot",x)
[1] "R Totorial"

Case insensitive replace:

> gsub("tut","ot",x,ignore.case=T))
[1] "R otorial"

If ignore.case is not set to True, no replace take place:

> gsub("tut","ot",x)
[1] "R Tutorial"
> x <- "line 4322: He is now 25 years old, and weights 130lbs"
> y <- gsub("\\d+","---",x)
> y
[1] "line ---: He is now --- years old, and weights ---lbs"
> x <- "line 4322: He is now 25 years old, and weights 130lbs"
> y <- gsub("[[:lower:]]","-",x)
> y
[1] "---- 4322: H- -- --- 25 ----- ---, --- ------- 130---"

Vector replacement:

> x <- c("R Tutorial","PHP Tutorial", "HTML Tutorial")
> gsub("Tutorial","Examples",x)
[1] "R Examples"    "PHP Examples"  "HTML Examples"

Regular Expression Syntax:

Syntax Description
\\d Digit, 0,1,2 ... 9
\\D Not Digit
\\s Space
\\S Not Space
\\w Word
\\W Not Word
\\t Tab
\\n New line
^ Beginning of the string
$ End of the string
\ Escape special characters, e.g. \\ is "\", \+ is "+"
| Alternation match. e.g. /(e|d)n/ matches "en" and "dn"
Any character, except \n or line terminator
[ab] a or b
[^ab] Any character except a and b
[0-9] All Digit
[A-Z] All uppercase A to Z letters
[a-z] All lowercase a to z letters
[A-z] All Uppercase and lowercase a to z letters
i+ i at least one time
i* i zero or more times
i? i zero or 1 time
i{n} i occurs n times in sequence
i{n1,n2} i occurs n1 - n2 times in sequence
i{n1,n2}? non greedy match, see above example
i{n,} i occures >= n times
[:alnum:] Alphanumeric characters: [:alpha:] and [:digit:]
[:alpha:] Alphabetic characters: [:lower:] and [:upper:]
[:blank:] Blank characters: e.g. space, tab
[:cntrl:] Control characters
[:digit:] Digits: 0 1 2 3 4 5 6 7 8 9
[:graph:] Graphical characters: [:alnum:] and [:punct:]
[:lower:] Lower-case letters in the current locale
[:print:] Printable characters: [:alnum:], [:punct:] and space
[:punct:] Punctuation character: ! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ _ ` { | } ~
[:space:] Space characters: tab, newline, vertical tab, form feed, carriage return, space
[:upper:] Upper-case letters in the current locale
[:xdigit:] Hexadecimal digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F a b c d e f

REF:

http://www.endmemo.com/program/R/gsub.php

http://cran.r-project.org/web/packages/stringr/stringr.pdf

http://stackoverflow.com/questions/11936339/in-r-how-do-i-replace-text-within-a-string

最新文章

  1. LCS记录
  2. 在linux下Java的环境配置
  3. c中的函数
  4. SqlServer 查看事务锁及执行语句
  5. Sample Join Analysis
  6. iOS 使用Keychain 保存 用户名和密码到 本地
  7. Windows下Nginx+Tomcat整合的安装与配置
  8. 函数lock_rec_get_first
  9. C++的XML编程经验――LIBXML2库使用指南[转]
  10. ZT: WEB学习资料
  11. 转Android 用Animation-list实现逐帧动画
  12. MySQL中的行级锁,表级锁,页级锁
  13. [luogu2051][bzoj1801][AHOI2009]chess中国象棋【动态规划】
  14. 【CF461E】Appleman and a Game 倍增floyd
  15. SpringCloud+Feign环境下文件上传与form-data同时存在的解决办法
  16. 天猫魔盒1代TMB100E刷机, 以及右声道无声的问题
  17. 新手必看ES6基础
  18. MySQL 索引 INDEX
  19. 我对android davilk 虚拟机的理解
  20. [AngularJS] Angular 1.3 ngMessages with ngAnimate

热门文章

  1. hdu-2045 递归
  2. [MeetCoder] Count Pairs
  3. the-implementation-of-epoll
  4. 【转】(六)unity4.6Ugui中文教程文档-------概要-UGUI Animation Integration
  5. 光照渲染[Unity]
  6. Python 操作redis 常用方法
  7. maven 打包时提示 软件包 xxxxxxx 不存在
  8. kali kvm Requested operation is not valid: network &#39;default&#39; is not active
  9. iOS中大文件下载(单线程下载)
  10. Phrase-Based &amp; Neural Unsupervised Machine Translation基于短语非监督机器翻译