Java正则表达式逃脱角色 [英] Java Regex Escape Characters

查看:113
本文介绍了Java正则表达式逃脱角色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习正则表达式,并在实现中遇到麻烦。

I'm learning Regex, and running into trouble in the implementation.

我发现 RegexTestHarness on Java教程,并运行它,以下字符串正确识别我的模式:

I found the RegexTestHarness on the Java Tutorials, and running it, the following string correctly identifies my pattern:

[\d|\s][\d]\.

(我的模式是任何两位数,或以空格开头的任何单个数字,后跟一段时间。)

(My pattern is any double digit, or any single digit preceded by a space, followed by a period.)

该字符串是由代码中的这一行获得的:

That string is obtained by this line in the code:

Pattern pattern = 
        Pattern.compile(console.readLine("%nEnter your regex: "));

当我尝试在Eclipse中编写一个简单的类时,它告诉我转义序列无效,并且我不会编译,除非我将字符串更改为:

When I try to write a simple class in Eclipse, it tells me the escape sequences are invalid, and won't compile unless I change the string to:

[\\d|\\s][\\d]\\.

在我班上我使用`Pattern pattern = Pattern.compile( );
当我将此字符串放回 TestHarness 时,它找不到正确的匹配项。

In my class I'm using`Pattern pattern = Pattern.compile(); When I put this string back into the TestHarness it doesn't find the correct matches.

有人能告诉我哪一个是正确的吗?某些格式的差异来自 console.readLine()

Can someone tell me which one is correct? Is the difference in some formatting from console.readLine()?

推荐答案

\ 是字符串文字中的特殊字符...。它用于转义其他特殊字符,或创建 \ n \ r <$ c $等字符c> \t 。

要在字符串文字中创建 \ 字符,可以在正则表达式引擎中使用通过在它之前添加另一个 \ 来逃避它(就像你在regex中那样,当你需要转义它的元字符如dot \。)。因此,表示 \ 的字符串将类似于\\

\ is special character in String literals "...". It is used to escape other special characters, or to create characters like \n \r \t.
To create \ character in string literal which can be used in regex engine you need to escape it by adding another \ before it (just like you do in regex when you need to escape its metacharacters like dot \.). So String representing \ will look like "\\".

当您从用户读取数据时,此问题不存在,因为您已经在阅读文字,因此即使用户将在控制台中写入 \ n 它将被解释为两个字符 \ n

This problem doesn't exist when you are reading data from user, because you are already reading literals, so even if user will write in console \n it will be interpreted as two characters \ and n.

在类字符<$中添加 | 也没有意义c $ c> [...] 除非您打算让该课程也匹配 | 字符,请记住 [abc] (a | b | c)相同,因此不需要 | in [\\d | \\ s]

Also there is no point in adding | inside class character [...] unless your intention is to make that class also match | character, remember that [abc] is the same as (a|b|c) so there is no need for | in "[\\d|\\s]".

这篇关于Java正则表达式逃脱角色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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