在PCRE中捕获未知重新定位的模式 [英] Capturing a pattern of unknown repitition in PCRE

查看:60
本文介绍了在PCRE中捕获未知重新定位的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于经验丰富的正则表达式表达者来说,这可能是一个快速的问题,但是我无法使我的比赛正确执行.

This may be a quick question for experienced regular expressionists, but I'm having trouble getting my match to execute correctly.

假设我有一个看起来像这样的字符串:

Suppose I had a string that looked like this:

http://aaa-bbbb-cc-ddddd-eee-.sub.dom

我想捕获所有的"aaa","bbbb","cc"和"ddddd"子字符串,但是我不确定会有多少个子字符串(例如,通过"zzz").

I would like to go capture all of the "aaa", "bbbb", "cc", and "ddddd" substrings, but I'm not sure how many there will be (e.g., having all triplets up through "zzz").

这是我现在要使用的正则表达式:

This is the regular expression I'm trying to use right now:

/http:\/\/(\w*?\-)+\.sub\.dom/

我这样写是因为:

  1. 我想匹配子字符串,但我希望每个子字符串在解析-时终止
  2. 我想捕获一个或多个这些子字符串

但是似乎只保存了它所做的 last 匹配(在上述情况下,它只会匹配"eee-".

But it seems to only be saving the last match that it makes (in the above case, it would only match "eee-".

是否有捕获所有匹配子字符串的好方法?

Is there a good way to capture all of the matched substrings?

更多信息:我正在使用PHP的PCRE函数preg_replace_callback.谢谢!

More information: I'm using PHP's PCRE function preg_replace_callback. Thanks!

推荐答案

否,无法匹配未知数量的捕获组.

No, it is not possible to match an unknown number of capture groups.

如果您尝试重复捕获组,则该组将始终包含最后捕获的值.

If you try to repeat a capture group, it will always contain the last value captured.

您能否更广泛地解释您要做什么?也许还有另一种简单的方法(可能没有正则表达式).

Could you explain a bit more broadly what you're trying to do? Perhaps there is another simple way to do it (possibly without regular expressions).

这篇关于在PCRE中捕获未知重新定位的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆