zoukankan      html  css  js  c++  java
  • 【转】说说{}与[]这两个符号有什么区别?

    【转】出自:  http://blog.51cto.com/lidao/1926390

    1.题目

    说说{}与[]这两个符号有什么区别?

    2.参考答案

    这两个看似简单的符号,其实内容还不少。我们一起来看看。

    2.1 通配符中

    通配符在linux中通常用来匹配/找文件名或目录名。
    最常用的就是 ls -l *.txt显示出所有以.txt结尾的文件。

    2.1.1  {} 花括号,大括号,生产序列
    [root@show regular]# echo {a..z} {0..9}
    a b c d e f g h i j k l m n o p q r s t u v w x y z 0 1 2 3 4 5 6 7 8 9

    用来生产数字,字母序列或者他们的组合

    [root@show regular]# echo stu{0..9}
    stu0 stu1 stu2 stu3 stu4 stu5 stu6 stu7 stu8 stu9
    2.1.2 []表示一个筐或括号表达式

    [abc]表示a或者b或者c,即字母abc中的任何一个
    [a-z]表示字母a到z中的任何一个字母

    [root@show test]# touch oldboy{0..20}.txt    
    [root@show test]# ls oldboy[0-9]*oldboy0.txt   
    oldboy12.txt  oldboy15.txt  oldboy18.txt  oldboy20.txt  oldboy4.txt  oldboy7.txt
    oldboy10.txt  oldboy13.txt  oldboy16.txt  oldboy19.txt  oldboy2.txt   oldboy5.txt  oldboy8.txt
    oldboy11.txt  oldboy14.txt  oldboy17.txt  oldboy1.txt   oldboy3.txt   oldboy6.txt  oldboy9.txt
    [root@show test]# ls oldboy[0-9].txt         
    oldboy0.txt  oldboy2.txt  oldboy4.txt  oldboy6.txt  oldboy8.txt
    oldboy1.txt  oldboy3.txt  oldboy5.txt  oldboy7.txt  oldboy9.txt

    2.2正则表达式中

    正则表达式主要用于在文件中查找内容。

    2.2.1 {n,m} 花括号 重复前面一个字符n次到m次

    也可以理解为 前一个符号连续出现至少n次,最多m次。

    [root@show test]# egrep "o{1,3}" /server/files/regular/oldboy.txt 
    I am oldboy teacher!
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://oldboy.blog.51cto.com
    our size is http://blog.oldboyedu.com
    not 4900000448.
    my god ,i am not oldbey,but OLDBOY! 
    [root@show test]# cat /server/files/regular/oldboy.txt
    I am oldboy teacher!
    I teach linux.
    
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://oldboy.blog.51cto.com
    our size is http://blog.oldboyedu.com
    my qq is 49000448not 4900000448.
    my god ,i am not oldbey,but OLDBOY! 
    [root@show test]# egrep "0{1,3}" /server/files/regular/oldboy.txt 
    my qq is 49000448
    not 4900000448.
    [root@show test]# egrep -o "0{1,3}" /server/files/regular/oldboy.txt 
    000
    000
    00

    表示重复数字0至少1次最多3次。
    或者数字0连续出现了至少1次最多3次。

    2.2.2 [] 表示筐  [abc] 表示a或者b或者c

    其实在正则表达式中和通配符中[]的意思是类似的。都表示一个筐  筐里面的东西[abc] a或者b或者c.
    [oldboy]表示的是oldboy这几个字字母的任何一个字母

    [root@show test]# grep "[abc]" /server/files/regular/oldboy.txtI am oldboy teacher!
    I teach linux.
    I like badminton ball ,billiard ball and chinese chess!
    my blog is http://oldboy.blog.51cto.com
    our size is http://blog.oldboyedu.com
    my god ,i am not oldbey,but OLDBOY! 
    [root@show test]# grep -o "[abc]" /server/files/regular/oldboy.txta
    b
    a
    c
    a
    c
    b
    a
    b
    a
    b
    a
    b
    a
    a
    c
    c
    b
    b
    b
    c
    c
    b
    b
    c
    a
    b
    b

    2.3 总结

    {}最常用的就是生成序列echo {a..z}
    []最常用的功能是正则表达式中的筐,表示或者 [abc] [a-z] [0-9]

    3.统计信息

    今天是每日一题陪伴大家的第51天期待你的进步

    对于题目和答案的任何疑问,请在博客评论区留言
    往期题目索引

    http://lidao.blog.51cto.com/3388056/1914205

    【老鸟分享】Linux命令行终端提示符多种实用技巧!
    https://mp.weixin.qq.com/s/g-bX6WVjJubv3ShY7jqs0Q

  • 相关阅读:
    Leetcode Substring with Concatenation of All Words
    Leetcode Divide Two Integers
    Leetcode Edit Distance
    Leetcode Longest Palindromic Substring
    Leetcode Longest Substring Without Repeating Characters
    Leetcode 4Sum
    Leetcode 3Sum Closest
    Leetcode 3Sum
    Leetcode Candy
    Leetcode jump Game II
  • 原文地址:https://www.cnblogs.com/linyushuan/p/9893982.html
Copyright © 2011-2022 走看看