正则表达式“\\ p {Z}”是什么意思?

时间:2021-07-01 22:29:46

I am working with some code in java that has an statement like

我正在使用java中的一些代码,它具有类似的语句

String tempAttribute = ((String) attributes.get(i)).replaceAll("\\p{Z}","")

I am not used to regex, so what is the meaning of it? (If you could provide a website to learn the basics of regex that would be wonderful) I've seen that for a string like

我不习惯正则表达式,所以它的含义是什么? (如果你能提供一个网站来学习那些非常棒的正则表达式的基础知识)我已经看到了一个像

ept as y it gets transformed into eptasy, but this doesn't seem right. I believe the guy who wrote this wanted to trim leading and trailing spaces maybe.

因为它变成了eptasy,但这似乎并不正确。我相信写这篇文章的人想要修剪前导空格和尾随空格。

2 个解决方案

#1


8  

It removes all the whitespace (replaces all whitespace matches with empty strings).

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

A wonderful regex tutorial is available at regular-expressions.info. A citation from this site:

一个精彩的正则表达式教程可以在regular-expressions.info上找到。来自这个网站的引用:

\p{Z} or \p{Separator}: any kind of whitespace or invisible separator.

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

#2


3  

The OP stated that the code fragment was in Java. To comment on the statement:

OP表示代码片段是Java。对声明发表评论:

\p{Z} or \p{Separator}: any kind of whitespace or invisible separator.

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

the sample code below shows that this does not apply in Java.

下面的示例代码显示这不适用于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

The output for this is:

这个输出是:

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}
            ^

This shows that in Java \\p{Z} removes only spaces and not "any kind of whitespace or invisible separator".

这表明在Java \\ p {Z}中只删除空格而不是“任何类型的空格或不可见的分隔符”。

These results also show that in Java \\p{Separator} throws a PatternSyntaxException.

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

#1


8  

It removes all the whitespace (replaces all whitespace matches with empty strings).

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

A wonderful regex tutorial is available at regular-expressions.info. A citation from this site:

一个精彩的正则表达式教程可以在regular-expressions.info上找到。来自这个网站的引用:

\p{Z} or \p{Separator}: any kind of whitespace or invisible separator.

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

#2


3  

The OP stated that the code fragment was in Java. To comment on the statement:

OP表示代码片段是Java。对声明发表评论:

\p{Z} or \p{Separator}: any kind of whitespace or invisible separator.

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

the sample code below shows that this does not apply in Java.

下面的示例代码显示这不适用于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

The output for this is:

这个输出是:

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}
            ^

This shows that in Java \\p{Z} removes only spaces and not "any kind of whitespace or invisible separator".

这表明在Java \\ p {Z}中只删除空格而不是“任何类型的空格或不可见的分隔符”。

These results also show that in Java \\p{Separator} throws a PatternSyntaxException.

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