使用正则表达式将单引号字符串中的所有双引号转出 [英] Escape all double quotes inside a single quoted string with Regex

查看:919
本文介绍了使用正则表达式将单引号字符串中的所有双引号转出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

正则表达式在单引号内转义双引号

我需要一个正则表达式(没有其他语言!!,最好是Perl语法REGEX或PCRE语法REGEX)将所有双引号替换为 \在一个单引号的字符串内。这是一个示例字符串(文件的一部分):

I need a regex (no other language!!, best would be perl syntax REGEX or PCRE syntax REGEX) to replace all double quotes " with a \" that are inside a single quoted string. This is an example string (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">';

(请注意:他们不必配对someValueBetween,所以它可能有不均匀的数字一个单引号的双引号。)

(Be aware: They dont have to be paired "someValueBetween" so its possible that there are uneven numbers of double quotes in one single quoted string.)

这应该是上面最后一行的最终结果:

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

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

提前感谢

***更新:
为了说清楚,我只想要一个正则表达式,而不是一个perl程序。正则表达式可以是perl regex语法或PHP PCRE语法(这是一个非常接近的语法,从我理解的perl正则表达式语法)。目标是您可以在IDES中运行正则表达式来搜索和替换支持正则表达式的菜单(如Eclipse和PhpEd fe)!!

***Update: To make it clear, i want a regular expression only, not a perl programm. The regular expression can be perl regex syntax or PHP PCRE syntax (which is a very close syntax to the perl regex syntax from what i understand). Goal is that you can run the regex in IDES in the search and replace menus that support regex's (like Eclipse and PhpEd f.e )!!

换句话说,我想要一个正则表达式,我将放入搜索IDE字段,使我完全将所有未转义的在单引号字符串中作为结果。在eclipse的替换字段中,我可以只是放

In other words, i want a regex that i will put in the search IDE field that gives me exactly all unescaped " in the single quoted string as a result. In the replace field of eclipse i can then just put \$1 to escape them.

他们应该在Regexbuddy或正则表达式教练中工作,所以我可以测试他们。

They should work in Regexbuddy or regex coach please so i can test them.

至少这是计划:)

推荐答案

你要求Perl(或PCRE),没有别的。

You asked for Perl (or PCRE) and nothing else.

Ok。

如果您只想逃避未转义的双引号,无论您在哪里找到它们,请执行以下操作:

If you just want to escape unescaped double quotes no matter where you find them, do this:

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

如果要在未转义的单引号之间避免未转义的双引号,而您只有一对这样的单引号,这样做:

If you want to escape unescaped double quotes between unescaped single quotes, and you only have one pair of such single quotes, do this:

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;

但是,如果您每行可能有多组配对的未转义单引号,并且您只想逃避那些未转义的单引号之间的未转义的双引号,那么请执行以下操作:

But if you may have multiple sets of paired unescaped single quotes per line, and you only want to escape the unescaped double quotes that fall between those unescaped single quotes, then do this:

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;

注意这一切都假定你没有合法配对的非双引号包含未转义的单引号。即使这样的事情也会让你失望:

Note that this all presupposed you have no legitimate paired unescaped double quotes containing unescaped single quotes. Even something like this will throw you off:

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

尽管如此,您想要使用正确的解析模块。

Probably, though, you want to use a proper parsing module.

请不要注意所有的肮脏和诡异的SO着色。由于某些原因,似乎无法解析Perl以及perl。无法想象为什么☺

Please pay no attention to all the garish and deceitfully incorrect SO coloring. For some reason, it doesn't seem to be able to parse Perl as well as perl does. Can't imagine why. ☺

这篇关于使用正则表达式将单引号字符串中的所有双引号转出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆