Replace patterns that are inside delimiters using a regular expression call

后端 未结 5 1060
轮回少年
轮回少年 2021-01-07 02:13

I need to clip out all the occurances of the pattern \'--\' that are inside single quotes in long string (leaving intact the ones that are outside single quotes). <

5条回答
  •  独厮守ぢ
    2021-01-07 02:34

    This cannot be done with regular expressions, because you need to maintain state on whether you're inside single quotes or outside, and regex is inherently stateless. (Also, as far as I understand, single quotes can be escaped without terminating the "inside" region).

    Your best bet is to iterate through the string character by character, keeping a boolean flag on whether or not you're inside a quoted region - and remove the --'s that way.

提交回复
热议问题