【问题标题】:Regular expression to extract texts between square brackets [duplicate]正则表达式提取方括号之间的文本[重复]
【发布时间】:2012-04-28 15:39:44
【问题描述】:

我想从方括号之间提取文本,我正在使用[(.*?)],但它不能完全工作。

4 月 17 日 13:01:34 10.11.26.83 2012-04-17 00:30:52.222 -0700 ABCD XYZ ALER SOME_TEXT 99.99.182.1 49923 99.99.83.74 80 阻止策略 GLOBAL DENY NONE [type=" SOME TEXT" pattern="some-random-pattern" token="/function()"] GET 99.99.83.74/index.html/function() HTTP "-" "Wget/1.12 (linux-gnu) " 99.99.182.1 49923 "-" "-"

我想提取粗体文本。执行此操作的正则表达式是什么?

【问题讨论】:

  • 不能完全工作是什么意思?我使用了您的正则表达式,并且您已经在第 1 组中获得了所需的匹配项。
  • 看起来好像你的错误在其他地方,因为你使用的正则表达式是正确的。
  • 那么吃饱了. 怎么样?我记得,它不吃\n。也许我错了

标签: regex


【解决方案1】:

@rprakash:

模式剖析

<!--
\[(.*?)\]

Match the character “[” literally «\[»  
Match the regular expression below and capture its match into backreference number 1 «(.*?)»  
   Match any single character that is not a line break character «.*?»  
      Between zero and unlimited times, as few times as possible, expanding as needed (lazy) «*?»  
Match the character “]” literally «\]»  
-->

在使用 REGEX 时,使用 否定字符类 而不是 dot 运算符总是安全的。

正如 Bohemian 所说,它可能是 \[[^\]]+\],或者使用 \[(?#if grouping required)([^\]\[]+)\] 可能更安全。

为了在这个主题中更好地立足visit here

【讨论】:

    【解决方案2】:

    试试这个:

    \[[^\]]+\]
    

    [^\]] 是“除 ] 以外的任何字符”的字符类

    【讨论】:

      猜你喜欢
      • 2011-01-25
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多