简体   繁体   English

Scala:匹配特殊字符

[英]Scala: matching special characters

In Scala I need to test if a string has any of the following 'special' characters: !@#\\$^%&*()_-\\+={}[]|;:"'<,>.?/ 在Scala中,我需要测试字符串是否具有以下“特殊”字符: !@#\\$^%&*()_-\\+={}[]|;:"'<,>.?/

I can not simply use 'nonword' "\\\\W" regex for this because string may have Cyrillic characters that regex "\\\\W" matches as well. 我不能为此简单地使用'nonword' "\\\\W"正则表达式,因为字符串也可能具有与正则表达式"\\\\W"匹配的西里尔字符。 Trying to use regex: 尝试使用正则表达式:

new Regex("""~`!@#\$^%&*()_-\+={}[]|;:"'<,>.?/""")

results in exception: 结果例外:

 java.util.regex.PatternSyntaxException: Illegal repetition near index 17 ~`!@#\$^%&*()_-\+={}[]|;:"'<,>.?/
                                                                                           ^
at java.util.regex.Pattern.error(Pattern.java:1924)
at java.util.regex.Pattern.closure(Pattern.java:3104)
at java.util.regex.Pattern.sequence(Pattern.java:2101)

Any ideas? 有任何想法吗?

You need to put all the special characters into character class. 您需要将所有特殊字符放入字符类。

[~!@#$^%&*\\(\\)_+={}\\[\\]|;:\"'<,>.?`/\\\\-]

If you want to add space also, then it should be 如果您还想添加空间,那么应该

[~!@#$^%&*\\(\\)_+={}\\[\\]|;:\"'<,>.?` /\\\\-]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM