zoukankan      html  css  js  c++  java
  • 正则表达式-举例说明

    1. *前一个字符匹配0次或多次

        grep "a*" data.txt      ##筛选data.txt文档中包括0,1,或多个与a相关的行

        grep "aa*" data.txt    ##匹配包括一个/多个a相关的行

        grep "aaa*" data.txt  ##匹配包括二个/多个a相关的行

        2.“.”匹配除换行符外的任意字符

        grep "s..d" data.txt   ##匹配s与d字母间,有两个字符的单词

        grep "s.*d" data.txt  ##匹配s与d间任意零个或多个字符的单词

        grep ".*" data.txt      ##匹配所有内容

        3."^"行首    “$”行尾

        grep "^S" data.txt    ##匹配以S开头的行

        grep "n$" data.txt    ##匹配以n结尾的行

        grep -n 列出行,如grep -n  "^S" data.txt 匹配行数及具体行

        4.[]匹配中括号中的任意一个字符,只匹配一个字符

        grep s[ao]id -n data.txt   ##匹配said或soid

        grep [0-9] -n data.txt      ##匹配任意一个数字

        grep ^[^a-z] data.txt     ##匹配不含小写字母的行

        grep ^[^a-zA-Z] data.txt    ##匹配不含字母的行

        5.   "" 转义符

        grep ".$" data.txt     ##匹配以.结尾的行

        grep ".$" data.txt     ##除空白行以外的行

        6. a{n} 代表前面连续出现n个a的行

        grep "a{3}" data.txt    ##包含连续三个a的行

        grep "[0-9]{3}" data.txt  ##包含有连续三个数字的行

        7. {n,}  代表至少包含n个的行

        grep "[0-9]{3,}" data.txt  ##出现连续三个以上数字的行

        a{n,m}b    匹配a与b间包含n到m的行

  • 相关阅读:
    leecode 240. 搜索二维矩阵 II
    leecode 103. 二叉树的锯齿形层序遍历
    leecode 362. 敲击计数器
    leecode 152. 乘积最大子数组
    leecode 560. 和为K的子数组
    面试题 08.12. 八皇后
    leecode 450. 删除二叉搜索树中的节点
    leecode 384. 打乱数组
    leecoode 138. 复制带随机指针的链表
    mysql基本指令2
  • 原文地址:https://www.cnblogs.com/mncasey/p/7806560.html
Copyright © 2011-2022 走看看