0

With the test string:

This is a string1 string2 string3 string4 string5 string6 string7 so it is.

Is it possible to get all the stringXs as backreferences? Currently what I've tried ends up overwriting each time so I end up with one match - the last one.

For instance using a regex like this:

/This is a (?:(string\d) )+so it is./

Would always end up with a match of string7 at the end.

The best I've found so far is to remove the ?: above and explode on the spaces, but I'd be interested in whether there's a way to do it purely with regex.

update specifically to PHP, what would be the best way to achieve this?

6
  • 1
    It cannot be done with "regex", since any regex is used in code. What is yours? Note that getting all captures is only available in few languages. Commented May 25, 2018 at 14:30
  • The minimal example is using preg_match with the caluss above in PHP but I didn’t want to limit it to a language. I guess I could have specified PCRE, but I’m really just asking about regex. Commented May 25, 2018 at 14:33
  • What's the flavor/environment?
    – SamWhan
    Commented May 25, 2018 at 14:35
  • @Aran-Fey ahh you’re right I’ve been looking to find a similar answer but was failing. I should probably delete this that answers it nicely. Commented May 25, 2018 at 14:35
  • 1
    No need to delete - you can mark your own question as a duplicate of that. Your question can serve as a signpost for other people with the same problem.
    – Aran-Fey
    Commented May 25, 2018 at 14:48

1 Answer 1

1

You may use this regex using \G:

(?:This is a|\G(?!\A))\h+((?=.*so it is\.)string\d+)
  • \G asserts position at the end of the previous match or the start of the string for the first match
  • (?!\A) is negative lookahead to assert that we don't match \G at the line start
  • (?=.*so it is\.) is positive lookahead to assert that we have so it is. ahead of current position in input

RegEx Demo

Code:

$re = '/(?:This is a|\G(?!\A))\h+((?=.*so it is\.)string\d+)/m';
$str = 'This is a string1 string2 string3 string4 string5 string6 string7 so it is.';

preg_match_all($re, $str, $matches);

// Print the entire match result
print_r($matches[1]);

Not the answer you're looking for? Browse other questions tagged or ask your own question.