简体   繁体   English

转义序列不正确

[英]Escape sequence incorrect

I have a pattern that I want to match, but the compiler says that it is incorrect. 我有一个要匹配的模式,但是编译器说这是不正确的。 How should I correct make the scape sequence in java? 我该如何正确地在Java中制作scape序列?

This is the pattern: 这是模式:

String patter = "\/\/s.ytimg.com\/yts\/jsbin\/html5player-en_US-vfllxLx6Z\/html5player.js"

The compiler complains because there is no such escape character as \\/ . 编译器抱怨是因为没有\\/这样的转义字符。 Common escape characters are these: 常见的转义字符是:

  • \\n newline \\n换行符
  • \\t tab \\t标签
  • ... ...

To embed a literal \\ in a string, you need to use \\\\ , for example: 要将文字\\嵌入字符串中,您需要使用\\\\ ,例如:

String pattern = "\\/\\/s.ytimg.com\\/yts\\/jsbin\\/html5player-en_US-vfllxLx6Z\\/html5player.js";

But actually, I don't really see why you need \\ there at all. 但是实际上,我真的不明白您为什么需要\\那里。 I think what you really need is most probably no \\ at all: 我认为您真正需要的根本不是\\

String pattern = "//s.ytimg.com/yts/jsbin/html5player-en_US-vfllxLx6Z/html5player.js";

If you want to escape \\ then you need to make it double like \\\\ . 如果要转义\\则需要使它像\\\\一样加倍。 You may try this: 您可以尝试以下方法:

String patter = "\\/\\/s.ytimg.com\\/yts\\/jsbin\\/html5player-en_US-vfllxLx6Z\\/html5player.js";

In any case where \\ is used, it needs to be duplicated, as \\ is used to escape special sequences, for example \\n to create a newline. 在任何情况下,使用\\都需要将其复制,因为\\用于转义特殊序列,例如\\n以创建换行符。 \\\\ is equivalent to a single \\ in the actual url. \\\\等于实际网址中的单个\\

For your actual URL, use this: 对于您的实际URL,请使用以下命令:

String patter = "\\/\\/s.ytimg.com\\/yts\\/jsbin\\/html5player-en_US-vfllxLx6Z\\/html5player.js"

However, if you were trying to escape the / character, that is unnecessary. 但是,如果您尝试转义/字符,则没有必要。 You can just use this: 您可以使用以下命令:

String patter = "//s.ytimg.com/yts/jsbin/html5player-en_US-vfllxLx6Z/html5player.js"

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

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