如何表示反斜杠 [英] How to represent backslash

查看:210
本文介绍了如何表示反斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想替换哪些不在提供的列表中

I want to replace which is not in provided list

[^-\\dA-Za-zÂÃâ`~!@#$%^&*()_+=[{]}:;<,>.?/ ]

我想在此列表中包含反斜杠 \ ,以便不会替换反斜杠。
如果我这样给出

I want to include backslash \ in this list, so that backslash wont be replaced. If I give like this

.replaceAll("[^-\\dA-Za-zÂÃâ`~!@#$%^&*()_+=[{]}\\:;<,>.?/ ]","")

它没有像我预期的那样工作。

its not working as I expected.

"xyda\asff"..replaceAll("[^-\\dA-Za-zÂÃâ`~!@#$%^&*()_+=[{]}\\:;<,>.?/ ]","")







Expected result :: xyda\asff
Output :: xydaasff

我不想替换 \

推荐答案

要在正则表达式中编写单个文字反斜杠,需要四个反斜杠(<$代码中的c $ c> \\\\ )(参见最后一个字符):

To code a single literal backslash in regex, you need four backslashes (\\\\) in the code (see last char):

[^-\\dA-Za-zÂÃâ`~!@#$%^&*()_+=[{]}:;<,>.?/ \\\\]

每对反斜杠都是字符串中的一个反斜杠,你需要两个这些在正则表达式中进一步逃脱;你需要逃脱逃脱。

Each pair of backslashes is a single backslash in the string, and you need two of those so it is further escaped in the regex; you need to escape the escape.

我们是否会逃避这种编码眼中的java? (努力争取双关语)

Will we ever escape this coding eyesore in java? (trying hard for the pun there)

这篇关于如何表示反斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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