Escaping all double quotes inside a single quote string with Regex

Possible duplicate:
Regular expression to avoid double quotes inside single quotes

I need a regular expression (there is no other language !!, the best would be REGX or PCRE REGEX syntax) to replace all double quotes "with ones \"that are inside the same quote. This is an example line (part of a file):

var baseUrl = $("#baseurl").html();
var head = '<div id="finishingDiv" style="background-image:url({baseUrl}css/userAd/images/out_main.jpg); background-repeat: repeat-y; ">'+
'<div id="buttonbar" style="width:810px; text-align:right">';

(Keep in mind: they do not need to be paired with "someValueBetween", so it’s possible that odd numbers of double quotes are listed on the same line.)

This should be the end result for the last line above:

'<div id=\"buttonbar\" style=\"width:810px; text-align:right\">';

Thanks in advance

*** : , , perl. regex perl PHP PCRE ( regex perl , ). , IDES , (, Eclipse PhpEd f.e)!

, , IDE , unescaped " . eclipse \$1, .

Regexbuddy regex coach, , .

, :)


+3
3

Perl ( PCRE) .

Ok.

, , , :

  s{
      (?<! (?<! \\ ) \\{1} )
      (?<! (?<! \\ ) \\{3} )
      (?<! (?<! \\ ) \\{5} )
      (?<! (?<! \\ ) \\{7} )
      (?= " )
  }{\\}xg;

, , , :

1 while s{

  (?(DEFINE)

    (?<unescaped>
      (?<! (?<! \\ ) \\{1} )
      (?<! (?<! \\ ) \\{3} )
      (?<! (?<! \\ ) \\{5} )
      (?<! (?<! \\ ) \\{7} )
    )

    (?<single_quote> (?&unescaped) ' )
    (?<double_quote> (?&unescaped) " )
    (?<unquoted>     [^'] *?          )

  )

  (?<HEAD>
    (?&single_quote)
    (?&unquoted)
  )

  (?<TAIL>
    (?&double_quote)
    (?&unquoted)
    (?&single_quote)

  )

}<$+{HEAD}\\$+{TAIL}>xg;

, , , :

sub escape_quote {
  my $_ = shift;
  s{
      (?<! (?<! \\ ) \\{1} )
      (?<! (?<! \\ ) \\{3} )
      (?<! (?<! \\ ) \\{5} )
      (?<! (?<! \\ ) \\{7} )
      (?= " )
  }{\\}xg;

  return $_;
}

s{

  (?(DEFINE)

    (?<unescaped>
      (?<! (?<! \\ ) \\{1} )
      (?<! (?<! \\ ) \\{3} )
      (?<! (?<! \\ ) \\{5} )
      (?<! (?<! \\ ) \\{7} )
    )

    (?<single_quote> (?&unescaped) ' )
    (?<unquoted>     [^'] *?          )

  )

  (?<HEAD> (?&single_quote) )
  (?<TARGET> (?&unquoted) )
  (?<TAIL> (?&single_quote) )

}{
               $+{HEAD}    .
  escape_quote($+{TARGET}) .
               $+{TAIL}

}xeg;

, , . - :

my $cute = q(') . "stuff" . q(');

, , .

SO. - , , Perl, perl. , . ☺

+4

, IDE . . , , (Perl, Java, Python ..) . IDE .

UPDATE:. , Visual Studio .NET-, Notepad ++ Boost. /IDE, , Visual Studio. (.NET , , .)

JEdit IntelliJ IDEA, Java, Java regex flavor, . Visual Studio .NET; . TextMate, Mac, Apple , Oniguruma, Notepad ++ ( Windows, ) - !

, , , , . , :

: \G((?:(?:\A|')[^']*+')?+[^'"]*+)"([^'"]*+)

: $1\\"$2

(, , , , - , ( ) , .)

\G ( ) , , , JavaScript Python. (*+, ?+) , ; PCRE, Oniguruma, Perl 5.10+ Java..NET , , .

generic-regex , . , , - JGSoft : EditPad Pro, PowerGrep RegexBuddy. JAGSoft-regex- , , ; , , .

p.s. , Eclipse ; , , Java regex (, , ICU, Java), .

+2

​​ ( ), ( sed):

s|'\([^'"]*\)"\([^']*\)'|'\1\"\2'|g
0

Source: https://habr.com/ru/post/1772554/


All Articles