【问题标题】:Regex InOgham unicode in javaJava 中的正则表达式 InOgham unicode
【发布时间】:2023-02-21 00:23:09
【问题描述】:

我需要删除收到的字符串中的以下字符:

U+0020 (space)
U+00A0 (no-break space)
U+1680 (ogham space mark)
U+2000 (en quad)
U+2001 (em quad)
U+2002 (en space)
U+2003 (em space) 
U+2004 (three-per-em space) 
U+2005 (four-per-em space) 
U+2006 (six-per-em space) 
U+2007 (figure space) 
U+2008 (punctuation space) 
U+2009 (thin space) 
U+200A (hair space) 
U+202F (narrow no-break space) 
U+205F (medium mathematical space) 
U+3000 (ideographic space) 
U+2028 (line separator) 
U+2029 (paragraph separator)

我发现大多数列出的,我可以使用以下正则表达式删除:

\p{Zs}|\p{Zl}|\p{Zp}

这应该涵盖除 U+1680 之外的所有内容。根据this site,模式为奥格姆空间标记应该是\p{InOgham},但是当我去正则表达式测试网站上测试时,没有人接受它。 是否有具体的表达方式,或者\p{Zs} 也涵盖了它?

【问题讨论】:

  • InOgham 是whole ogham block。无论如何,\u1680 可能是一个安全的选择。

标签: java regex unicode


【解决方案1】:

Unicode 字符 U+1680 是一个 Ogham 空格标记,它不同于标准空格字符 (U+0020) 和您列出的其他空格字符。

在 Java 的正则表达式中,p{Z} 字符类表示任何 Unicode 分隔符,其中包括您列出的除 U+1680 之外的所有空格字符。因此,正则表达式p{Zs}|p{Zl}|p{Zp} 应该匹配您列出的除 U+1680 之外的所有空格字符。

要精确匹配 Ogham 空格标记 (U+1680),您可以使用 p{InOgham} 字符类,它匹配 Ogham Unicode 块中的任何字符。下面是一个匹配您列出的所有空格字符(包括 U+1680)的正则表达式模式示例:

[p{Zs}p{Zl}p{Zp}p{InOgham}]

您可以将此模式与 String.replaceAll() 方法调用结合使用,以从字符串中删除所有出现的这些空格字符:

String input = "some string with spaces and u1680 ogham space marks";
String output = input.replaceAll("[\p{Zs}\p{Zl}\p{Zp}\p{InOgham}]", "");
System.out.println(output); // prints "somestringwithspacesandoghamspacemarks"

【讨论】:

    猜你喜欢
    • 2013-03-02
    • 2014-05-15
    • 1970-01-01
    • 2021-07-01
    • 2016-04-22
    • 2013-04-07
    • 1970-01-01
    • 2010-09-06
    • 1970-01-01
    相关资源
    最近更新 更多