正则表达式课堂笔记
Tim Chen(motion$) Lv5

写在开头的话

题:最近我在炼数成金社区上《R七种武器之网络爬虫RCurl》这一门课,然后三周的短课程在最后一周就提到了正则表达式。刚好我在大二的时候有听过传智播客的老师讲过,还有点印象,听起课来还蛮舒服的,倒也觉得这个东西还是挺重要的,它就像是一个模式,用以解决一类事情,如过滤邮箱地址等,故我现在做个笔记,以博客的形式。

对一个新事物学习的模式

1
2
3
(What is it)是什么?
(How to do it)怎么做?
(Why we need it)为什么?

正则表达式是什么?

字符串的操作(R语言)

  • 由于正则表达式是字符串的一些规则,所以我们首先温习一下字符串的基本操作
    • 赋值
    • 长度:nchar、字符串个数:length
    • 替换:chartr(原始字符、替换字符、字符串)
    • 连接:paste 参数sep,collapse
    • 切割:strsplit
    • 比较:>、<、==、!=
    • 并集、交集、补集:union,intersect,setdiff
    • 截取:sustr,substring
    • 匹配:match,pmatch,charmatch
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      53
      54
      55
      56
      57
      58
      59
      60
      61
      62
      63
      64
      65
      66
      67
      68
      69
      70
      71
      72
      73
      74
      75
      76
      77
      78
      79
      80
      81
      82
      83
      84
      85
      86
      87
      88
      89
      90
      91
      92
      93
      94
      95
      96
      97
      98
      99
      # 赋值
      hello <- "Hello World!"
      hello
      [1] "Hello World!"

      # 长度:nchar
      nchar(hello)
      [1] 12
      # 字符串个数:length
      hello2 <- c("Hello","World!")
      length(hello2)
      [1] 2

      # 替换:chartr(原始字符、替换字符、字符串)
      # 原始字符和替换字符的长度一一对应,如例:e对1,l对2,o对3
      chartr("elo","123",hello)
      [1] "H1223 W3r2d!"
      hello
      [1] "Hello World!"

      # 连接:paste 参数sep,collapse
      # sep=""表示字符串间无缝结合
      paste("Hello","World!",sep="")
      [1] "HelloWorld!"
      # 不设sep参数,默认字符串间是空格
      paste("Hello","World!")
      [1] "Hello World!"

      # 字符串切割strsplit
      # 第一个参数为要切割的字符串,第二个参数为字符串的切割值
      str="My/name/is/Tim/Chan!"
      strsplit(str,"/")
      [[1]]
      [1] "My" "name" "is" "Tim" "Chan!"

      # 字符串的比较
      # 字符串的比较准则:从左到右逐个字符按照字母表顺序一一对比
      a = "a"
      b = "b"
      a > b
      [1] FALSE
      b > a
      [1] TRUE
      c = "ab"
      d = "aaaaaaa"
      c >d
      [1] TRUE
      a == b
      [1] FALSE

      # 并集、交集、补集:union,intersect,setdiff
      # 高中集合知识温习,假设有a,b两个集合
      # 并集:所有a集合和所有b集合的元素组成的新的集合,重复元素只出现一次
      # 交集:取既在a集合也在b集合的元素组成的集合
      # 补集:setdiff(a,b),取在a中的元素但不在b中的元素组成的集合
      a = c("a","b","c","d","e")
      b = c("c","d","e","f","g")
      # 取并集
      union(a,b)
      [1] "a" "b" "c" "d" "e" "f" "g"
      # 取交集
      intersect(a,b)
      [1] "c" "d" "e"
      # 取补集
      setdiff(a,b)
      [1] "a" "b"
      setdiff(b,a)
      [1] "f" "g"

      # 截取:substr,substring
      # substr(x, start, stop)
      # substring(text, first, last = 1000000L)
      # 准备字符串
      strs
      [1] "abcdefghijklmnopqrstuvwxyz"
      # 第一种比较,没有不同
      substr(strs,2,5)
      [1] "bcde"
      substring(strs,2,5)
      [1] "bcde"
      # 第二种比较
      # substring截取的字符串分布对应位置:2-5,3-6,4-7,5-8
      # 而substr只处理第一个位置2-5
      substring(strs,2:5,5:8)
      [1] "bcde" "cdef" "defg" "efgh"
      substr(strs,2:5,5:8)
      [1] "bcde"
      # 第三种比较,没有指定stop参数
      # substring默认为渠道字符串末尾
      # substr则报错
      substring(strs,2)
      [1] "bcdefghijklmnopqrstuvwxyz"
      substr(strs,2)
      Error in substr(strs, 2) : argument "stop" is missing, with no default

      # 匹配 match,pmatch,charmatch
      # matcht:完整匹配
      # pmatch,charmatch部分匹配

More info:百度百科

正则表达式的简单定义

  • 正则表达式主要是对字符串操作的一系列规则。

正则表达式入门

  • :转义字符
  • .:除了换行以外的任意字符
  • ^:一行字符串的起始
  • $:一行字符串的结束
  • *:零个或多个字符
  • +:一个或多个字符
  • ?:零个或者一个字符
  • 保留字符都需要在前面加上转义字符来表示

正则表达式怎么实现?

为什么我们需要正则表达式?

后记

 评论