Regex match everything up to first period

前端 未结 3 691
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 02:51

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 j

3条回答
  •  清歌不尽
    2020-12-17 03:27

    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
    

提交回复
热议问题