Regex to first occurrence only?

前端 未结 4 1119
暖寄归人
暖寄归人 2020-11-29 11:18

Let\'s say I have the following string:

this is a test for the sake of testing. this is only a test. The end.

and I want to se

4条回答
  •  余生分开走
    2020-11-29 12:04

    * is a greedy quantifier. That means it matches as much as possible, i.e. what you are seeing. Depending on the specific language support for regex, you will need to find a non-greedy quantifier. Usually this is a trailing question mark, like this: *?. That means it will stop consuming letters as soon as the rest of the regex can be satisfied.

    There is a good explanation of greediness here.

提交回复
热议问题