I am trying to replace all occurrences of ANY value between two single quotes, including escaped single quotes. The following works quite well, except when an escaped single quote is found. This makes sense, but is there a way around it. I want to replace ALL characters between single quotes.
$ echo "'blah\'blah'" | perl -pe s/"'"[^"'"]*"'"/stuff/g
stuffblah'
I would like to just see:
stuff
Inside of our quotes, we want to allow the following tokens
[^']or\', encoded as\\'or\\, encoded as\\\\. We need this extra rule to represent all possible data.The or in regexes is denoted by the pipe character
|, and we bracket these possible tokens with parens to group them for our iteration:or perhaps more clearly showing what can be escaped:
Now we just pack this into quotes, and make the grouping parens non-saving (no variables
$1etc need to be created), and we haveNote the use of the
/xmodifier to include non-semantic whitespace for better readability.Edit: Perl one-liners
All one-liners were tested unter
bash. Other shells will display other quoting horrors.In the next line, the unescaped regex is provided.
This one-liner matches single quotes, as specified:
doubling the backslashes is done to accomodate for shell escaping.
This one-liner matches double quotes: