use regex to find based on 2 characters but only replace one

  • I’d like to find all instances of a comma that are followed by a non-whitespace character, and then replace the comma with a line break but leave the non-whitespace character alone.

    It’s easy enough to find these matches using


    But I can’t figure out how to only replace the comma portion of the match… or even what the name of the approach/technique is so that I can read up on it. Any advice?

    (I worked around the problem for now by replacing comma followed by space with another symbol, then replacing all commas, then replacing the other symbol with comma + space again, but that’s a workaround I’d like to skip in the future.)

  • @aworkerb

    Very much like what was discussed HERE earlier today, so there are some links there to the docs that will help with your understanding.

    For you case I’d suggest [,](?=\S)

    Not sure why you made the comma a single thing inside the [ and ], but I didn’t change that part in my suggestion.

  • lookbehind and lookahead assertions … aren’t part of the “real” match

    That’s the concept I wasn’t able to name.

    Not sure why you made the comma a single thing inside the [ and ] …

    Because I barely know what I’m doing and was piecing it together from possibly applicable Stack Exchange posts.

    Anyway, thanks, you nailed it!

Log in to reply