需要匹配的python正则表达式(某些字符串)[另一个字符串]

时间:2015-10-13 19:56:16

标签: python regex

我正在寻找可以在python中使用的正则表达式,从一串文本中解析多个reddit样式的链接。这种链接的格式是:(描述符文本)[URL]。

要解析的示例文本可以是这样的:

  

[google] string0(google.com)string1 [gmail](gmail.com)string2   [hotmail](hotmail.com)string3

从上面的文字中,我想解析以下字符串:

  1. [Gmail的](gmail.com)
  2. [Hotmail的](hotmail.com)
  3. 我一直在尝试使用\ [(。*?)\]和\((。*?)\)组合的正则表达式变体,但是已经产生了很多误报。会很感激的建议。

1 个答案:

答案 0 :(得分:2)

您可以使用此正则表达式:

\[[^]]+\]\([^\)]+\)

解释

\[        # the open '['
[^]]+     # at least one non ']' character
\]        # the end ']'
\(        # the open '('
[^\)]+    # at least one non ')' character
\)        # the end ')'

希望它有所帮助。

Regex live here.