tags:

views:

41

answers:

1

Is it possible to get all regular expression matches in PHP? I need a script that will, for example, match .+ in abc and give the result:

Array(a, b, c, ab, bc, abc)

+1  A: 

I'm not sure there is a defined set of "all matches" in a regular expression.

For example, what if your pattern were .+.+? What is matched by the first .+? What is matched by the second?

A string may match a particular RE binding in multiple different ways, and which substring is captured by different parts of the RE may depend on things like greedy vs. non-greedy matching. But there is no defined way to iterate over all the different possible captures. You'd have to dramatically change the way that REs are processed to do this.

Borealid
`.+.+` on `abc` would give Array(`ab`, `bc`, `abc`)
Tom
I understand your answer, but I am still hoping for a solution, like a custom library that can answer my prayer :)
Tom