How to replace the first occurrence of a regular expression in Python?

前端 未结 2 1841
逝去的感伤
逝去的感伤 2020-11-29 06:08

I want to replace just the first occurrence of a regular expression in a string. Is there a convenient way to do this?

2条回答
  •  再見小時候
    2020-11-29 06:44

    Specify the count argument in re.sub(pattern, repl, string[, count, flags])

    The optional argument count is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer. If omitted or zero, all occurrences will be replaced.

提交回复
热议问题