Regex, proper escaping for \" (groovy)

214
January 01, 2021, at 11:20 AM

My second day coding java/groovy so bare with me :)

In short, I'm trying to replace \" with \\"

string = string.replaceAll()

I've tried with any number of backslashes, and as I expect with 0-1 backslashes it catches any " in the string, but so it does also with two or three. With four I can match \\\\", again as I'd expect. Why is this and what's the solution?

My actual aim is to parse merge request changes, where the diff field has the raw version with \" and \n which causes problems with JsonSlurper. I managed to deal with linebreaks using

string = string.replaceAll(/\\\n/, /\\\\\n/)

but with quotation mark the same approach just doesn't seem to work.

Thanks for help.

Edit: For extra clarity teststring = ' Zero " One \" Two \\" ' I haven't been able to change case One without also changing Zero. Case two is handled by 4 backslashes.

Rent Charter Buses Company
READ ALSO
How to create a nested array from current array in PHP?

How to create a nested array from current array in PHP?

I am trying to read the categories in an XML file from my API providerThe only problem is that they nested the subcategories in a string:

187
Configuring sqlalchemy engine with user containing “:”

Configuring sqlalchemy engine with user containing “:”

I am trying to configure an engine in sqlalchemy to connect with temporary credentials from an AWS IAM role using get_cluster_credentials apiWhen I do so this is the user I get 'IAM:user_rw'

216
Transform is not applied on Embedded SVGs Chrome

Transform is not applied on Embedded SVGs Chrome

I am new to using SVGs, but everything was fine until I tried my code on chrome and found that it doesn't work properly, all transform attributes are neglectedFirefox gets it done as expected

40
JavaScript Memory Model with Process Memory model

JavaScript Memory Model with Process Memory model

I'm studying Process Memory model and got curious with JavaScript's memory model

162