Marc
Marc

Reputation: 27

Regexp: group for every found item (PCRE)

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:

Upvotes: 1

Views: 357

Answers (2)

rubber boots
rubber boots

Reputation: 15194

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

Upvotes: 2

M'vy
M'vy

Reputation: 5774

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.

Upvotes: 1

Related Questions