简体   繁体   English

Java正则表达式; 需要将\\'映射到''

[英]Java Regular Expression; need to map \' to ''

I'm trying to map the \\' to '' . 我正在尝试将\\'映射到'' That is, a string consisting of a backslash followed immediately by a single quote should be mapped to two single quotes. 也就是说,由反斜杠后紧跟一个单引号组成的字符串应映射到两个单引号。

I've tried using 我试过使用

string = string.replace("\'", "''")

but this also maps a single quote to two single quotes (ie, "'" to "''" ), which is incorrect for what I need. 但这也将单引号映射到两个单引号(即"'""''" ),这对于我需要的是不正确的。 What am I doing wrong? 我究竟做错了什么?

这里的反斜杠是一个转义字符,它将与'如果您要匹配\\'您需要\\\\'匹配:

string = string.replace("\\'", "''")

The "\\" needs to be escaped itself because it is a special character. “ \\”本身必须转义,因为它是一个特殊字符。 "\\\\'", "''"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM