【问题标题】:Regex return string between two matches [duplicate]两个匹配项之间的正则表达式返回字符串[重复]
【发布时间】:2017-04-22 11:20:38
【问题描述】:

我正在尝试获取两个特定匹配项之间的字符串,例如两个主题标签。

我想要达到的目标:

输入:some text in front ##hi there## some text behind

输出:hi there

使用/%%(.*)%%/,返回##hi there##。

提前致谢

【问题讨论】:

  • 语言是什么?
  • @WiktorStribiżew 对不起,我不知道这是否相关。这是PHP
  • 访问$matches[1]

标签: php regex preg-match


【解决方案1】:

你需要向前看,向后看。

(?<=##).*(?=##)

这将匹配hi there

https://regex101.com/r/qUR4NR/1

【讨论】:

  • 如果我想要多个匹配项,比如说字符串是there %%are%% two %%matches%% now
猜你喜欢
  • 2017-08-31
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多