如何在模式结束时首先停止匹配?

时间:2015-04-11 05:05:00

标签: regex

我有这样的代码:

super dooper pooper but dooper super pooper

我需要匹配从superpooper(可以包含所有字符)的所有内容,但请停留在第一个pooper

我该怎么做?

2 个答案:

答案 0 :(得分:0)

正如评论中所说的那样:/super.*?pooper/如果你不关心跨线的匹配,就会有所作为。如果您需要跨行匹配(并且您在评论中说过),那么您需要:

/super.*?pooper/s

这意味着:

  • / = delimiter
  • super =匹配super
  • .*? =在"非贪婪"中匹配零个或多个字符时尚(尽快停止)
  • pooper =匹配pooper
  • / = delimiter
  • s =让.匹配换行符(\n

Here's a demo

答案 1 :(得分:0)

试试这个正则表达式:

super[\s\S]*?pooper

super  ... Match super
[\s\S] ... Match a single character that is a whitespace character or
           match a single character that is not a whitespace character (all characters).
*?     ... Between zero and unlimmited times as few times as possible.
pooper ... Match pooper