Remove first occurrence of comma in a string

后端 未结 5 1439
甜味超标
甜味超标 2020-12-18 20:51

I am looking for a way to remove the first occurrence of a comma in a string, for example

\"some text1, some tex2, some text3\"

should retu

5条回答
  •  粉色の甜心
    2020-12-18 21:54

    A simple one liner will do it:

    text = text.replace(/^(?=(?:[^,]*,){2})([^,]*),/, '$1');
    

    Here is how it works:

    regex = re.compile(r"""
        ^                # Anchor to start of line|string.
        (?=              # Look ahead to make sure
          (?:[^,]*,){2}  # There are at least 2 commas.
        )                # End lookahead assertion.
        ([^,]*)          # $1: Zero or more non-commas.
        ,                # First comma is to be stripped.
        """, re.VERBOSE)
    

提交回复
热议问题