java正则表达式单引号双引号,使用正则表达式将单引号字符串中的所有双引号转出...

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 = '

'+

'

';

(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:

'

';

Thanks in advance

***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 )!!

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.

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

At least that is the plan :)

解决方案

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{

(?

(?

(?

(?

(?= " )

}{\\}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) ' )

(? (?&unescaped) " )

(? [^'] *? )

)

(?

(?&single_quote)

(?&unquoted)

)

(?

(?&double_quote)

(?&unquoted)

(?&single_quote)

)

}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{

(?

(?

(?

(?

(?= " )

}{\\}xg;

return $_;

}

s{

(?(DEFINE)

(?

(?

(?

(?

(?

)

(? (?&unescaped) ' )

(? [^'] *? )

)

(?

(?&single_quote) )

(? (?&unquoted) )

(? (?&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.

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. ☺

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值