11

Trying to get a lazy regex match of everything up until the first period of a sentence.

e.g. Just want to get "jack and jill." from this sentence:
"jack and jill. went up the hill. to fetch a pail."

/.+\./ matches the whole sentence (example)
/(.+?\.)/ matches each instance (example)

Is there a way to just match the first instance?

Yarin
  • 173,523
  • 149
  • 402
  • 512

3 Answers3

31
/^([^.]+)/

Let's break it down,

  • ^ is the newline anchor

  • [^.] this matches any character that's not a period

  • \+ to take until a period

And the expression is encapsulated with () to capture it.

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59
  • 1
    *"^ is the beginning of string anchor"*: No it's wrong, in Ruby `^` matches the start of a line (not of the string). The anchor for the start of the string is `\A` – Casimir et Hippolyte Jun 01 '17 at 17:57
  • 3
    Note that when `.` appears within square brackets, it does not need to be escaped. `[\.]` is the same as `[.]`. (And the negating carat doesn't change this.) – CAustin Jun 01 '17 at 18:01
  • You're correct, I never have a reason to use \A so the concept blends together for me. Updated to reflect this. – Matthew Brock Carey Jun 01 '17 at 18:01
2

If you only want the first occurrence, do not choose the scan method that returns all results in the string. You can use the match method that returns a MatchData instance, but you can also simply write:

> "jack and jill. went up the hill. to fetch a pail."[/.+?\./]
 => "jack and jill."
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
1

I would be inclined to use a regex, but there are other options.

str = "jack and jill. went up the hill. supposedly to fetch a pail of water."
str[0..i] if i = str.index('.')
  #=> "jack and jill."

str = "three blind mice"
str[0..i] if i = str.index('.')
  #=> nil
Cary Swoveland
  • 106,649
  • 6
  • 63
  • 100