2

I have a specific regular expression, \Good .+\.\. To my understanding that means, match each pattern that starts with "Good ", then any number of word characters (one or more) and finally end with a dot ('.').

So "Good morning." could is a pattern that this regex is matching, also "Good afternoon.", "Good day.", etc. But somehow it also matches the pattern "Good morning. Good afternoon. Good day." as a whole.

How is this possible?

Themelis
  • 2,929
  • 1
  • 11
  • 31
  • 2
    You need to make the `.+` lazy by adding a `?` i.e. `.+?` otherwise it absorbs all characters (including `.`) up to the final `.`. Alternatively use, `[^.]+` to only match non-`.` characters – Nick Feb 10 '20 at 00:30
  • To answer the first comment @Nick I am not sure if it answers my question since I am learning them at the momment. – Themelis Feb 10 '20 at 00:33
  • You might want to take a look at https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean – Nick Feb 10 '20 at 00:34
  • Maybe I should have asked differently, like what regex matches the pattern `"Good ."`. the `` are not included in the pattern of course. – Themelis Feb 10 '20 at 00:36
  • 2
    Perhaps `Good\s[a-zA-Z]+\.` or if you use the `i` (case-insensitive) flag `good\s[a-z]+\.`? – Nick Feb 10 '20 at 00:38
  • By the way I think you're right, `.+` absorbs the final `\.`. – Themelis Feb 10 '20 at 00:38
  • You're right, if you could provide an answer so I could mark it as correct apart from thanking you @Nick – Themelis Feb 10 '20 at 00:40
  • 1
    One of the regex gold badge holders will eventually close this as a dupe so no point answering. Thanks are sufficient. – Nick Feb 10 '20 at 00:44

1 Answers1

1

As @Nick noted, .+ absorbs the final \.. I believe it's an example of a greedy expression where an expression tries to match the longest possible string.

Themelis
  • 2,929
  • 1
  • 11
  • 31