首先,我想删除字符串中的所有标点符号。我写了下面的代码。
\nPattern pattern = Pattern.compile("\\\\p{Punct}");\nMatcher matcher = pattern.matcher("!\\"#$%&\'()*+,-./:;<=>?@[\\\\]^_`{|}~\xef\xbc\x88hello\xef\xbc\x89");\nif (matcher.find())\n System.out.println(matcher.replaceAll(""));\nRun Code Online (Sandbox Code Playgroud)\n替换后我得到了这个输出:\xef\xbc\x88hello\xef\xbc\x89。
!"#$%&\'()*+,-./:;<=>?@[\\]^_因此该模式与{|}~`之一匹配,它与官方文档匹配。
但我也想删除“\xef\xbc\x88”Fullwidth Left Parenthesis U+FF08*和“\xef\xbc\x89” Fullwidth Right Parenthesis U+FF09,所以我将代码更改为:
Pattern pattern = Pattern.compile("(?U)\\\\p{Punct}");\n Matcher matcher = pattern.matcher("!\\"#$%&\'()*+,-./:;<=>?@[\\\\]^_`{|}~\xef\xbc\x88\xef\xbc\x89");\n if (matcher.find())\n System.out.println(matcher.replaceAll(""));\nRun Code Online (Sandbox Code Playgroud)\n替换后,我得到以下输出:$+<=>^|~`
它确实匹配了 "\xef\xbc\x88"Fullwidth Left Parenthesis U+FF08*和 "\xef\xbc\x89" Fullwidth Right Parenthesis U+FF09,但它错过了$+<=>^|~`。
我感到很困惑。为什么会发生这种事?有人可以提供一些帮助吗?
\n