Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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/

I wrote it this way because:

  1. I want to match substrings, but I want each to terminate when a - is parsed
  2. I want to capture one or more of these substrings

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
535 views
Welcome To Ask or Share your Answers For Others

1 Answer

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...