Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace outer single quotes (') to double (") and inner double (") to single qoutes (')

I've got something like this:

$class = 'class="cl"' . " style=" . '"color:black,' . "background:red;" . '"';

Is there any simple way by regex to format this code to " ' ' " style? I mean this:

$class = "class='cl'" . " style=" . "'color:black," . "background:red;" . "'";

I tried this regex:

'(?!(([^"]"){2})[^"]*$)

But this replaces outer single quotes ' only. How can I also replace inner double quotes by single quotes?

like image 965
newtonrus Avatar asked Jun 08 '18 14:06

newtonrus


People also ask

How to replace single quotes with double quotes in a string?

Use the String.replaceAll () method to replace single with double quotes, e.g. const replaced = str.replaceAll ("'", '"');. The replaceAll method will return a new string where all occurrences of single quotes are replaced with double quotes. We pass the following parameters to the String.replaceAll method: a substring to match in the string.

What is the difference between single and double quotes?

Double quotes ( ") in a sentence are used around a direct quote. Direct quote refers to a text or speech that someone else said or wrote. Single quotes ( ') in a sentence are used to indicate quotations inside of other quotations. There are numerous ways to replace single quotes with double quotes.

How do I get rid of double quotes in word?

Click Proofing, and then click AutoCorrect Options. In the AutoCorrect dialog box, do the following: Click the AutoFormat As You Type tab, and under Replace as you type, select or clear the "Straight quotes" with the “smart quotes” check box. ... Click OK. Punctuation: What is the difference between single quotes and double quotes?

How do you type single quotes in a sentence?

If you want to end up with typographically-correct single quotes, use Find and Replace (Ctrl-H). Consider the following simple sentence: "Okay," John said, "let's see how this works!" Type a " symbol in the Find what box. Type a ' symbol in the Replace with box. Click Replace All.


2 Answers

You could do this in three steps, where only the first one needs a regular expression:

  1. Replace each outer quote (whatever kind -- single or double) with a non-used character (e.g. µ), a pair at a time:

    Find: ('|")(((?!\1).)*).
    Repl: µ\2µ

  2. Replace each remaining double quote with a single quote, since by consequence of step 1 they are inner:

    Find: "
    Repl: '

  3. Replace each occurrence of the special character (of step 1) with a double quote

    Find: µ
    Repl: "

Evidently the first (and only) regular expression does the major magic: it captures the first quote it finds (single or double) and then captures characters up to the next occurrence of the same kind of quote (using the \1 back-reference in a negative condition and eventually matching that second occurrence with .). The condition for this to work is that all outer quotes come in pairs.

When you launch the first replacement, make sure that your caret is at the very start of the text so it starts outside of any quote pair.

NB: PHP (which you seem to use, but it's true also in several other languages) allows quotes with string literals to be escaped with backslashes. The task would become a bit more complex if such escapes were present in your input, but it is possible.

like image 182
trincot Avatar answered Nov 15 '22 04:11

trincot


My own solution. There are 4 regex for Notepad++.

1. Find all double-quotes (") inside two single-quotes (') and replace them with tilde (~):

RegEx: (?:\G(?!^)|([^']*(?:'[^'"]*'[^']*)*'))[^"']*\K"([^"']*+(?:'(?1)|$))?
Replacement: ~$2

2. Find all single-quotes (') not surrounded by two double-quotes (") and replace them with backquote (`):

RegEx: '(?=(?:[^"]*"[^"]*")*[^"]*$)
Replacement: `

3. Find all tildes (~) inside two backquotes (`) and replace them with single-quote ('):

RegEx: (?:\G(?!^)|([^`]*(?:`[^`~]*`[^`]*)*`))[^~`]*\K~([^~`]*+(?:`(?1)|$))?
Replacement: '$2

4. And last step, all tildes (~) and backquotes (`) to replace with double-quotes ("):

RegEx: ~|`
Replacement: "

Tildes and backquotes should not be appeared in code before first step. Thanks to link1 and link2

like image 24
newtonrus Avatar answered Nov 15 '22 05:11

newtonrus