zoukankan      html  css  js  c++  java
  • 正则表达式

    反斜线,转义特殊字符
    $ cat data2 
    The cost is $4.00 
    $ sed -n '/$/p' data2 
    The cost is $4.00
    锚字符
    ^匹配行首
    $ cat data3 
    This is a test line. 
    this is another test line. 
    A line that tests this feature. 
    Yet more testing of this 
    $ sed -n '/^this/p' data3 
    this is another test line.
    $匹配行尾
    [root@localhost ~]# sed -n '/this$/p' data3.txt 
    Yet more testing of this
    .点号用来匹配除换行符以外的任意单个字符。
    [root@localhost ~]# cat data6.sh 
    The cat is sleeping. 
    That is a very nice hat. 
    This test is at line four. 
    at ten o'clock we'll go home.
    匹配任何at的文本
    [root@localhost ~]# sed -n '/.at/p' data6.sh 
    The cat is sleeping. 
    That is a very nice hat. 
    This test is at line four.
    []匹配字符组,
    [root@localhost ~]# sed -n '/[ch]at/p' data6.sh 
    The cat is sleeping. 
    That is a very nice hat. 
    chat 
    可以在单个表达式中用多个字符组。
    $ echo "Yes" | sed -n '/[Yy][Ee][Ss]/p' 
    Yes 
    $ echo "yEs" | sed -n '/[Yy][Ee][Ss]/p' 
    yEs 
    $ echo "yeS" | sed -n '/[Yy][Ee][Ss]/p' 
    yeS 
    $
    
    匹配数字
    $ cat data7 
    This line doesn't contain a number. 
    This line has 1 number on it. 
    This line a number 2 on it. 
    This line has a number 4 on it. 
    $ sed -n '/[0123]/p' data7 
    This line has 1 number on it. 
    This line a number 2 on it.
    
    $ cat data8 
    60633
    46201 
    223001 
    4353 
    22203
    $ sed -n ' 
    >/[0123456789][0123456789][0123456789][0123456789][0123456789]/p 
    >' data8 
    60633 
    46201 
    223001 
    22203
    成功过滤掉了不可能是邮编的那些过短的数字,因为最后一个字符组
    没有字符可匹配。但它也通过了那个六位数,尽管我们只定义了5个字符组
    
    如果要确保只匹配五位数,就必须将匹配的字符和其他字符分开,要么用空格,要么指明行首和行尾
    $ sed -n ' 
    > /^
    [0123456789][0123456789][0123456789][0123456789][0123456789]$/p 
    > ' data8 
    60633 
    46201 
    22203
    排除型字符组,匹配组中没有的字符
    $ sed -n '/[^
    ch]at/p' data6 
    This test is at line four.
    区间匹配 -只需要指定区间的第一个字符、单破折线以及区间的最后一个字符就行了。正则表达式会包括此区间内的任意字符
    [root@localhost ~]# cat data8.txt 
    60633
    46201 
    223001 
    4353 
    22203
    
    [root@localhost ~]# sed -n '/^[0-9][0-9][0-9][0-9][0-9]$/p' data8.txt 
    60633
    22203
     如果字母出现在数据中的任何位置,这个模式都将不成立
    echo "a123" | sed -n '/^[0-9][0-9][0-9][0-9]$/p'
    
    匹配字母
    [root@localhost ~]# cat data6.sh 
    The cat is sleeping. 
    That is a very nice hat. 
    This test is at line four. 
    at at ten o'clock we'll go home.
    chat 
    
    [c-h]at匹配了首字母在字母c和字母h之间的单词。这种情况下,只含有单词at的行将无法匹配该模式。
    [root@localhost ~]# sed -n '/[a-ch-m]at/p' data6.sh 
    The cat is sleeping. 
    That is a very nice hat. 
    chat 
    该字符组允许区间a~c、h~m中的字母出现在at文本前,但不允许出现d~g的字母。
    
    
    该模式不匹配fat文本,因为它没在指定的区间。
    $ echo "I'm getting too fat." | sed -n '/[a-ch-m]at/p'
  • 相关阅读:
    工作中常用git命令总结
    工作中,实用map给数组去重的详解
    关于OC中的block自己的一些理解(一)
    存储过程专题(Oracle)
    ORACLE事物隔离级别和脏读、幻读、不可重复读区别
    C#客户端Json转DataTable
    C# Newtonsoft.Json JObject常用方法
    C#中的内部函数(子函数)
    C# Dev GridView当前行
    C#从数据库中加载照片的
  • 原文地址:https://www.cnblogs.com/YingLai/p/12145431.html
Copyright © 2011-2022 走看看