4

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)

Tom
  • 6,991
  • 13
  • 60
  • 78
  • I think this is what you want: http://stackoverflow.com/questions/2617055/how-to-generate-all-permutations-of-a-string-in-php – shamittomar Sep 01 '10 at 20:35
  • 1
    This is not what regular expressions are meant to do, and there is no reliable way to do this, besides writing your own (subset of) regex parser and doing something different then all other regex implementations. – Wrikken Sep 01 '10 at 21:00
  • @shamittomar No, I thought it was clear enough that `.+` was only an example. – Tom Sep 01 '10 at 21:44
  • @Wrikken Maybe you should post that comment as an answer. – Tom Sep 08 '10 at 12:39

3 Answers3

1

The issue is at the overlap, you want to match 'ab' and also 'bc' which you won't be able to do with a simple regex. However consider the following.

You can split out every character with either of these lines:

preg_match_all('/./', 'abc', $matches);
str_split('abc');

Giving you: array('a', 'b', 'c').

And the following will split pairs of characters with the remaining single character:

preg_match_all('/.{2}|./', 'abc', $matches);

Giving you: array('ab', 'c');

So you could play around with combinations/variations of these to achieve your outcome.

Andrew
  • 3,335
  • 5
  • 36
  • 45
1

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
  • 95,191
  • 9
  • 106
  • 122
  • `.+.+` on `abc` would give Array(`ab`, `bc`, `abc`) – Tom Sep 01 '10 at 20:35
  • I understand your answer, but I am still hoping for a solution, like a custom library that can answer my prayer :) – Tom Sep 01 '10 at 20:36
0

I know the question is a bit old, but there still might be someone who needs the answer.

Look at the flags parameter for preg_match_all function

burtek
  • 2,576
  • 7
  • 29
  • 37