3

I need to capture the optional texts in a pattern, with the extracted examples i, 9, Caffè and you in the following matched scenarios:

  • Valid matches:

    love!
    9 love!
    love Caffè!
    i love you!
    
  • Invalid matches:

    love
    iloveyou
    

My rule: [*] love [*]!, with [*] meaning blank text can be accepted to be captured.

I tried to make the regex ((?:.+) )?love( (?:.+))?! (online) but it failed to capture the correct texts.

MiP
  • 499
  • 1
  • 7
  • 16
  • What are the rules as to why you would match your valid examples, but not your invalid matches - There are quite a few ways to interpret this. – Theo Feb 26 '17 at 15:21
  • 4
    Well, maybe [`(?:(.+) )?love(?: (.+))?!`](https://regex101.com/r/aJWQHI/4) works as expected? – Wiktor Stribiżew Feb 26 '17 at 15:22
  • You say the texts are optional but then say "love" by itself is invalid. Well, which is it? Are you sure you understand your own requirements? – CrayonViolent Feb 26 '17 at 16:21
  • @CrayonViolent The "love" lacks the exclamation mark `!` after it. – MiP Feb 26 '17 at 16:22
  • @WiktorStribiżew Yes, thanks. I'm new to SO so I don't know how to close this question though. – MiP Feb 27 '17 at 02:57
  • @MiP Wiktor is meant to offer his solution as an answer so you can award it the green tick. You can't give a green tick to a comment. Wiktor has been here long enough to know that. – mickmackusa Feb 27 '17 at 03:14
  • @MiP: I was not sure that was all there was to it. I posted an answer so that you could mark it as accepted. – Wiktor Stribiżew Feb 27 '17 at 07:08

1 Answers1

2

You may swap your capturing with non-capturing groups:

(?:(.+) )?love(?: (.+))?!
 ^^            ^^

See the regex demo

Now, the text that you will have inside groups will be without spaces as they are part of the non-capturing groups that are only meant to group subpatterns.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Sorry to trouble you, but what if I want the character `!` optional, e.g.: https://regex101.com/r/ZptHNN/3 The current regex won't work correctly, like getting `you` from `i love you! not`. – MiP Feb 27 '17 at 11:05
  • Yes, the `!` is very important here since it is an obligatory subpattern and "anchors" the match on the right. What is the rule for the right hand boundary then? – Wiktor Stribiżew Feb 27 '17 at 11:07
  • Do you mean to make `(?: (.+))?` work correctly there should always be mandatory characters on the right instead of optionals? – MiP Feb 27 '17 at 11:11
  • Acc. to your comment, `love or not` is a valid match, `(?:(.+) )?love(?: (.+))?(?: or)? not` matches it. Why should it be invalid? – Wiktor Stribiżew Feb 27 '17 at 11:15
  • `loveornot` is a valid match, but `loveornot` isn't. – MiP Feb 27 '17 at 11:20
  • 1
    Well, you may fail the match if there are two consecutive whitespace symbols after `love` - `(?:(.+) )?love(?: (\S.*))?(?: or)? not` or `(?:(.+) )?love(?!\s{2})(?: (.+))?(?: or)? not` will work then. – Wiktor Stribiżew Feb 27 '17 at 11:22
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/136745/discussion-between-mip-and-wiktor-stribizew). – MiP Feb 27 '17 at 11:24