zoukankan      html  css  js  c++  java
  • bash中的grep温故知新

    几个有用的参数:

    -E--extended-regexp
    Interpret PATTERN as an extended regular expression (see below)..

    -e PATTERN--regexp=PATTERN
    Use PATTERN as the pattern; useful to protect patterns beginning with -.
    -v--invert-match
    Invert the sense of matching, to select non-matching lines.
     

    关于Regular Expressions的摘录:

    Grep understands two different versions of regular expression syntax: "basic" and "extended." In GNU grep, there is no difference in available functionality using either syntax. In other implementations 

    Finally, certain named classes of characters are predefined within bracket expressions, as follows. Their names are self explanatory, and they are [:alnum:][:alpha:][:cntrl:],[:digit:][:graph:][:lower:][:print:][:punct:][:space:][:upper:], and [:xdigit:]. For example, [[:alnum:]] means [0-9A-Za-z], except the latter form depends upon the C locale and the ASCII character encoding, whereas the former is independent of locale and character set. (Note that the brackets in these class names are part of the symbolic names, and must be included in addition to the brackets delimiting the bracket list.) Most metacharacters lose their special meaning inside lists. To include a literal ] place it first in the list. Similarly, to include a literal ^ place it anywhere but first. Finally, to include a literal - place it last.


    The symbol \b matches the empty string at the edge of a word, and \B matches the empty string provided it's not at the edge of a word. 

    The backreference \n, where n is a single digit, matches the substring previously matched by the nth parenthesized subexpression of the regular expression.

    这句话非常难以理解,用下面的例子来说明:

    后向引用

    使用小括号指定一个子表达式后,匹配这个子表达式的文本(也就是此分组捕获的内容)可以在表达式或其它程序中作进一步的处理。默认情况下,每个分组会自动拥有一个组号,规则是:从左向右,以分组的左括号为标志,第一个出现的分组的组号为1,第二个为2,以此类推。

    呃……其实,组号分配还不像我刚说得那么简单:

    • 分组0对应整个正则表达式
    • 实际上组号分配过程是要从左向右扫描两遍的:第一遍只给未命名组分配,第二遍只给命名组分配--因此所有命名组的组号都大于未命名的组号
    • 你可以使用(?:exp)这样的语法来剥夺一个分组对组号分配的参与权.

    后向引用用于重复搜索前面某个分组匹配的文本。例如,\1代表分组1匹配的文本。难以理解?请看示例:

    \b(\w+)\b\s+\1\b可以用来匹配重复的单词,像go go, 或者kitty kitty。这个表达式首先是一个单词,也就是单词开始处和结束处之间的多于一个的字母或数字(\b(\w+)\b),这个单词会被捕获到编号为1的分组中,然后是1个或几个空白符(\s+),最后是分组1中捕获的内容(也就是前面匹配的那个单词)(\1)。

    你也可以自己指定子表达式的组名。要指定一个子表达式的组名,请使用这样的语法:(?<Word>\w+)(或者把尖括号换成'也行:(?'Word'\w+)),这样就把\w+的组名指定为Word了。要反向引用这个分组捕获的内容,你可以使用\k<Word>,所以上一个例子也可以写成这样:\b(?<Word>\w+)\b\s+\k<Word>\b

    使用小括号的时候,还有很多特定用途的语法。下面列出了最常用的一些:

    表4.常用分组语法
    分类代码/语法说明
    捕获(exp)匹配exp,并捕获文本到自动命名的组里
    (?<name>exp)匹配exp,并捕获文本到名称为name的组里,也可以写成(?'name'exp)
    (?:exp)匹配exp,不捕获匹配的文本,也不给此分组分配组号
    零宽断言(?=exp)匹配exp前面的位置
    (?<=exp)匹配exp后面的位置
    (?!exp)匹配后面跟的不是exp的位置
    (?<!exp)匹配前面不是exp的位置
    注释(?#comment)这种类型的分组不对正则表达式的处理产生任何影响,用于提供注释让人阅读

    我们已经讨论了前两种语法。第三个(?:exp)不会改变正则表达式的处理方式,只是这样的组匹配的内容不会像前两种那样被捕获到某个组里面,也不会拥有组号。“我为什么会想要这样做?”——好问题,你觉得为什么呢?

  • 相关阅读:
    关于跨域,以及跨域的几种方式
    跨域资源共享 CORS 详解
    python 协程与go协程的区别
    查看Mysql正在执行的事务、锁、等待
    undo log,当前读和快照读,redo log----与mvcc
    如何查找MySQL中查询慢的SQL语句
    dbForge Studio 2020 for MySQL v9.0.338破解软件包下载
    阿里云数据盘挂载完整过程
    [原][python]递归遍历文件夹下所有小文件,并删除
    [转][数据结构]R树 RTree
  • 原文地址:https://www.cnblogs.com/welkinwalker/p/2085456.html
Copyright © 2011-2022 走看看