Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 什么是regex\\p{Z}”是;什么意思?_Java_Regex_Replaceall - Fatal编程技术网

Java 什么是regex\\p{Z}”是;什么意思?

Java 什么是regex\\p{Z}”是;什么意思?,java,regex,replaceall,Java,Regex,Replaceall,我正在使用java中的一些代码,其语句如下 String tempAttribute = ((String) attributes.get(i)).replaceAll("\\p{Z}","") 我不习惯正则表达式,那么它是什么意思呢?(如果你能提供一个网站来学习regex的基础知识,那就太好了)我已经看到了一个字符串,比如 ept as y它被转换为eptasy,但这似乎不对。我相信写这篇文章的人可能想删掉前导空格和尾随空格。它删除了所有空格(用空字符串替换所有空格匹配项) 一个精彩的正则表

我正在使用java中的一些代码,其语句如下

String tempAttribute = ((String) attributes.get(i)).replaceAll("\\p{Z}","")
我不习惯正则表达式,那么它是什么意思呢?(如果你能提供一个网站来学习regex的基础知识,那就太好了)我已经看到了一个字符串,比如


ept as y
它被转换为
eptasy
,但这似乎不对。我相信写这篇文章的人可能想删掉前导空格和尾随空格。

它删除了所有空格(用空字符串替换所有空格匹配项)

一个精彩的正则表达式教程可以在。 引文:

\p{Z}或\p{Separator}:任何类型的空白或不可见分隔符


OP声明代码片段是用Java编写的。就声明发表评论:

\p{Z}或\p{Separator}:任何类型的空白或不可见分隔符

下面的示例代码表明,这不适用于Java

public static void main(String[] args) {

    // some normal white space characters
    String str = "word1 \t \n \f \r " + '\u000B' + " word2"; 

    // various regex patterns meant to remove ALL white spaces
    String s = str.replaceAll("\\s", "");
    String p = str.replaceAll("\\p{Space}", "");
    String b = str.replaceAll("\\p{Blank}", "");
    String z = str.replaceAll("\\p{Z}", "");

    // \\s removed all white spaces
    System.out.println("s [" + s + "]\n"); 

    // \\p{Space} removed all white spaces
    System.out.println("p [" + p + "]\n"); 

    // \\p{Blank} removed only \t and spaces not \n\f\r
    System.out.println("b [" + b + "]\n"); 

    // \\p{Z} removed only spaces not \t\n\f\r
    System.out.println("z [" + z + "]\n"); 

    // NOTE: \p{Separator} throws a PatternSyntaxException
    try {
        String t = str.replaceAll("\\p{Separator}","");
        System.out.println("t [" + t + "]\n"); // N/A
    } catch ( Exception e ) {
        System.out.println("throws " + e.getClass().getName() + 
                " with message\n" + e.getMessage());
    }

} // public static void main
其输出为:

s [word1word2]

p [word1word2]

b [word1


word2]

z [word1    


word2]

throws java.util.regex.PatternSyntaxException with message
Unknown character property name {Separator} near index 12
\p{Separator}
            ^
这表明在Java\\p{Z}中,只删除空格,而不删除“任何类型的空白或不可见分隔符”


这些结果还表明,在Java\\p{Separator}中抛出一个PatternSyntaxException。

首先,
\p
意味着要匹配一个类,一个字符集合,而不是单个字符。作为参考,这是模式类的Javadoc

Unicode脚本、块、类别和二进制属性是用Perl中的\p和\p结构编写的\如果输入具有属性prop,则p{prop}匹配,而如果输入具有该属性,\p{prop}则不匹配

然后
Z
是字符类(集合、集合)的名称。在本例中,它是分隔符的缩写<代码>分隔符包含3个子类:
空格分隔符
(Zs)、
行分隔符
(Zl)和
段落分隔符
(Zp)

请参阅此处,了解这些类在此处包含哪些字符:或


更多文档:

那么第一个斜杠呢?第一个斜杠是一个转义字符,表示
p{Z}
是一个查找空白的正则表达式,而不仅仅是
p
{/code>、
Z
}
。反斜杠在程序代码中是加倍的,因为它是Java的字符串语法。Java编译器从中生成一个反斜杠,并将带有一个斜杠的字符串传递给正则表达式引擎。查看否它是正确的您可以看到它确实匹配所有空白,因此它使用
replaceAll()
\\s删除给定代码中的空白,但不匹配“\u00A0”(无中断空格字符)
\n\r\f
属于类别Cc,不包含在Z中,因此它们不会被替换