【问题标题】:Regular expression for matching integers inclosed within square brackets including whitespace [duplicate]用于匹配包含在方括号内的整数的正则表达式,包括空格[重复]
【发布时间】:2019-12-30 17:17:06
【问题描述】:
s="  afd [asd] [12 ] [a34]  [ -43 ]tt [+12]xxx"

正则表达式匹配应该返回[12, -43, 12]

re.findall(r'\[([+-]?\d+)]', s)

它不起作用。我不知道如何包含空格

【问题讨论】:

标签: regex


【解决方案1】:

允许前导和尾随空格:

\[\s*([+-]?\d+)\s*\]

Demo

【讨论】:

  • 谢谢它的工作。返回的对象没有符号 [12, 43, 12] ,任何帮助将不胜感激。
  • 固定使用\s而不是\W
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 1970-01-01
  • 1970-01-01
  • 2015-06-23
  • 2022-10-25
  • 2016-10-06
  • 2011-07-18
相关资源
最近更新 更多