代码之家  ›  专栏  ›  技术社区  ›  slhck

grep不显示结果,在线regex测试仪显示结果

  •  3
  • slhck  · 技术社区  · 15 年前

    我对grep的行为相当没有经验。我有一堆XML文件,其中包含如下行:

    <identifier type="abc">abc:def.ghi/g1234.ab012345</identifier>
    <identifier type="abc">abc:def.ghi/g5678m.ab678901</identifier>
    

    我想在斜杠后面得到标识符部分,并用 RegexPal :

    [a-z]\d{4}[a-z]*\.[a-z]*\d*
    

    它突出了我想要的一切。很完美。现在,当我在同一个文件上运行grep时,不会得到任何结果。正如我所说,我对grep的了解并不多,所以我尝试了所有不同的组合。

    grep [a-z]\d{4}[a-z]*\.[a-z]*\d* test.xml
    grep "[a-z]\d{4}[a-z]*\.[a-z]*\d*" test.xml
    egrep "[a-z]\d{4}[a-z]*\.[a-z]*\d*" test.xml
    grep '[a-z]\d{4}[a-z]*\.[a-z]*\d*' test.xml
    grep -E '[a-z]\d{4}[a-z]*\.[a-z]*\d*' test.xml
    

    我做错什么了?

    7 回复  |  直到 7 年前
        1
  •  8
  •   Meekohi    7 年前

    • [a-z] g
    • \d{4} 1234
    • [a-z]* .

    grep \d [0-9] [:digit:]

    egrep *

        2
  •  5
  •   Kobi    15 年前

    grep \d [0-9]

    $ grep -P "[a-z]\d{4}[a-z]*\.[a-z]*\d*" test.xml
    

    $ egrep "[a-z][0-9]{4}[a-z]*\.[a-z]*[0-9]*" test.xml
    
        3
  •  2
  •   James Anderson    15 年前

    Basic vs Extended Regular Expressions
       In basic regular expressions the meta-characters ?, +, {, |, (, and ) lose their
       special meaning; instead use the backslashed versions \?, \+, \{,  \|,  \(,  and
       \).
    
       Traditional  egrep  did  not  support  the  {  meta-character,  and  some  egrep
       implementations support \{ instead,  so  portable  scripts  should  avoid  {  in
       grep -E patterns and should use [{] to match a literal {.
    
       GNU  grep -E  attempts  to  support  traditional usage by assuming that { is not
       special if it would be the start of  an  invalid  interval  specification.   For
       example,  the  command  grep -E '{1'  searches  for  the two-character string {1
       instead of reporting a syntax error in the regular expression.   POSIX.2  allows
       this behavior as an extension, but portable scripts should avoid it.
    

        4
  •  1
  •   codaddict    15 年前

    $ cat file
    <identifier type="abc">abc:def.ghi/g1234.ab012345</identifier>
    
    # Use -P option to enable Perl style regex \d.
    $ grep -P  '[a-z]\d{4}[a-z]*\.[a-z]*\d*' file
    <identifier type="abc">abc:def.ghi/g1234.ab012345</identifier>
    
    # to get only the part of the input that matches use -o option:
    $ grep -P -o '[a-z]\d{4}[a-z]*\.[a-z]*\d*' file
    g1234.ab012345
    
    # You can use [0-9] inplace of \d and use -E option.
    $ grep -E -o '[a-z][0-9]{4}[a-z]*\.[a-z]*[0-9]*' file
    g1234.ab012345
    $ 
    
        5
  •  0
  •   Valentin H    15 年前

        6
  •  0
  •   Paweł Nadolski    15 年前

    [a-z]\d{4}[a-z]*\.[a-z]*\d*