任何符号的Java正则表达式?
是否有接受任何符号的正则表达式?
编辑:为了澄清我正在寻找的内容..我想构建一个正则表达式,它将接受任意数量的空格,并且它必须包含至少 1 个符号(例如,." ' $ £ 等)或(不排除或)至少 1 个字符。
Is there a regex which accepts any symbol?
EDIT: To clarify what I'm looking for.. I want to build a regex which will accept ANY number of whitespaces and the it must contain atleast 1 symbol (e.g , . " ' $ £ etc.) or (not exclusive or) at least 1 character.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。点 (
.
) 将匹配任何符号,至少如果您将其与Pattern.DOTALL
标志(否则它不会匹配换行符)。来自文档:关于您的编辑:
这里有一个建议:
\s*
任意数量的空白字符\S+
一个或多个(“至少一个”)非空白字符。Yes. The dot (
.
) will match any symbol, at least if you use it in conjunction withPattern.DOTALL
flag (otherwise it won't match new-line characters). From the docs:Regarding your edit:
Here is a suggestion:
\s*
any number of whitespace characters\S+
one or more ("at least one") non-whitespace character.在Java中,符号是
\pS
,它与标点符号\pP
不同。我谈论这个问题,并枚举所有 ASCII 标点符号的类型,在此答案中。
像
[\p{Alnum}\s]
这样的模式仅适用于 20 世纪 60 年代的遗留数据集。要使用 Java 本机字符集进行工作,您需要一些类似的东西,我很抱歉 Java 使得使用现代数据集变得如此困难,但至少这是可能的。
只是不要询问边界或字素簇。为此,请参阅我的其他帖子< /a>.
In Java, a symbol is
\pS
, which is not the same as punctuation characters, which are\pP
.I talk about this issue, plus enumerate the types for all the ASCII punctuation and symbols, here in this answer.
Patterns like
[\p{Alnum}\s]
only work on legacy dataset from the 1960s. To work on things with the Java native characters set, you needs something on the order ofI’m sorry that Java makes it so difficult to work with modern dataset, but at least it is possible.
Just don’t ask about boundaries or grapheme clusters. For that, see my others posting.