1

Please see the example below as I'm not sure how to explain my ask.

What I have

(?<Name>\w+)(?:\slikes\s)(?<Likes>\w+)(?:\sand\s)?(?<Likes>\w+)

This will not work because of the re use of Likes group.

I need there to be 1 or 2 or even more likes in a single match.

Test Data

George likes icecream and pie

Paul likes ice

Adam likes apples and bananas

Jessica likes cherries

Juan likes cars

Emily likes violet

Expected Result

Match 1 Group 1: George

Match 1 Group 2: icecream

Match 1 Group 2: pie

Match 2 Group 1: Paul

Match 2 Group 2: ice

etc...

https://regex101.com/r/iJXHrc/1

  • 2
    If you are using Python, you may use PyPi regex module and enjoy access to `.captures("Likes")`. You do not have access to all captures in JS regex. – Wiktor Stribiżew May 30 '19 at 21:07
  • What language are you targetting? The solution for JavaScript and Python may be different. – p.s.w.g May 30 '19 at 21:07
  • The built-in `re` module doesn't support this use case, I don't believe. As @WiktorStribiżew said, look into the [`regex`](https://pypi.org/project/regex/) module – Green Cloak Guy May 30 '19 at 21:09
  • Why not just use unnamed groups and make the last group optional: [`(\w+)(?:\slikes\s)(\w+)(?:\sand\s(\w+))?`](https://regex101.com/r/iJXHrc/2). Besides, your current regex - even if you'd use dupe names eg in pcre - would [clip off letters on strings without `and`](https://regex101.com/r/iJXHrc/4) because of the optional `(?:\sand\s)?` betwween two word characters. – bobble bubble May 30 '19 at 21:26
  • `(?P\w+)\slikes\s(?P\w+)(?:\sand\s(?P\w+)(?:\sand\s(?P\w+)(?:\sand\s(?P\w+))?)?)?` https://regex101.com/r/6tgz8u/1 –  May 30 '19 at 21:32
  • I would advise using this "(?\w+)(?:\slikes\s)(?(?:\w+(?:\sand\s)?)+)". It will match one "likes" name. You can then split the likes on "\sand\s" and you'll get a list of what you want. – blackdrumb May 30 '19 at 21:33

0 Answers0