Could anybody please tell me how to write a regular expression to find every nth word?
Thank you very kindly,
Kelly
How to find every nth word?
Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard
-
ben_josephs
- Posts: 2464
- Joined: Sun Mar 02, 2003 9:22 pm
-
ben_josephs
- Posts: 2464
- Joined: Sun Mar 02, 2003 9:22 pm
Here's an improvement. With this regex the replacement will remove the residue of words after all the groups of 5 have been matched:
(?:(?:\w+\W+){4}(\w+)\W*|.+)
(This relies on the fact that in TextPad's (and most other) regex recognisers, alternation (...|...) is not greedy. The alternatives are tried one by one from the left. Once one of them has matched all subsequent ones are ignored, even if they might have found a longer match.)
(?:(?:\w+\W+){4}(\w+)\W*|.+)
(This relies on the fact that in TextPad's (and most other) regex recognisers, alternation (...|...) is not greedy. The alternatives are tried one by one from the left. Once one of them has matched all subsequent ones are ignored, even if they might have found a longer match.)