【问题标题】:Another simple RegEx match另一个简单的正则表达式匹配
【发布时间】:2014-04-16 23:16:01
【问题描述】:

尝试获取正确的正则表达式语法以匹配标记的行(带 *):

#32 0 0 A [2] *
#13 3 2 A [1]
#44 1 2 A [2]
#44 0 0 A [1]
#44 0 0 A [2] *
#44 0 0 B [2] *

我想要的是包含“0 0”和“[2]”之后某处的行。任何字符都可以出现在这两条规则之前、中间或之后。

这就是我今天所拥有的,它找到了第一部分:

#.. 0 0

如何完成?

另外,如果有人想为像我这样的正则表达式新手分享一个好的 URL,我会更加感激!

【问题讨论】:

标签: regex


【解决方案1】:

`我想要的是包含“0 0”和之后某处“[2]”的行。任何字符都可以出现在之前、之间

你可以使用这个正则表达式:

^#[0-9]{2} 0 0.*?\[2\]

说明:

^ assert position at start of the string
# matches the character # literally
[0-9]{2} match a single character present in the list below
Quantifier: Exactly 2 times
0-9 a single character in the range between 0 and 9
 0 0 matches the characters  0 0 literally
.*? matches any character (except newline)
Quantifier: Between zero and unlimited times, as few times as possible, expanding as needed [lazy]
\[ matches the character [ literally
2 matches the character 2 literally
\] matches the character ] literally

【讨论】:

    【解决方案2】:

    你可以使用:

    #.. 0 0.*\[2\].*
    

    【讨论】:

      【解决方案3】:

      以下内容如何:

      echo '#32 0 0 A [2]
      #13 3 2 A [1]
      #44 1 2 A [2]
      #44 0 0 A [1]
      #44 0 0 A [2]
      #44 0 0 B [2]' | grep -E '.*0 0.*\[2\].*'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多