1

I am using PCRE with C++ (Borland) and want to get all matches of a group.

^(\w+\s)(\w+\s)*(\w+)$

input 1: first second third results in 3 groups (first, second and third)

input 2:first second second third results in 3 groups (first, second and third) too, but I need 4 groups.

The second word is optinal and occurs 0 - n times.

// EDIT:

Marc
  • 27
  • 4
  • I think this is not possible. See http://stackoverflow.com/questions/5018487/regular-expression-with-variable-number-of-groups – Jens Mar 09 '11 at 10:07
  • Not *directly* possible, but changing the second capture from `(\w+\s)*` to `((?:\w+\s)*)` would capture everything at the price of needing an additional splitting step on the second capture group to extract all the elements. – geekosaur Mar 09 '11 at 10:12

2 Answers2

2

PCRE seems to have a split function, so if you know your delimiters are a group of whitespace, you should split the text and, depending on the count of splitted fields, react accordingly.

Regards

rbo

rubber boots
  • 14,924
  • 5
  • 33
  • 44
  • Seems like Koders as something [here](http://www.koders.com/cpp/fid7DEB2114CF60DF5829CF1F805087FD2FA908CA73.aspx?s=http) on this topic – M'vy Mar 09 '11 at 10:29
1

I think your best shot is to match :

^(\w+\s)((?:\w+\s)*)(\w+)$

and then match the inside x words by hand, looking for \s with string comparison.

M'vy
  • 5,696
  • 2
  • 30
  • 43
  • No pb. If this is solved for you don't forget to vote up the relevant answerS and comments to reward the authors. And also mark your question as solved to close the topic. Thanks a lot. – M'vy Mar 09 '11 at 10:28