简体   繁体   English

检查字符串是否包含特殊字符(句点和问号Java除外)

[英]Check if a string contains special characters except period and question mark Java

I'm trying to find a way to use regex and match to see if a string contains a special character with the exception of a period or comma. 我正在尝试找到一种使用正则表达式并进行匹配的方法,以查看字符串是否包含特殊字符(句点或逗号除外)。 I'm not very familiar with regex and about to bang my head on a wall. 我对正则表达式不是很熟悉,正要把头撞在墙上。 What is the regex to when I call 我打电话给的正则表达式是什么

string.matches("??????");

will return a boolean based on whether string contains a special character other than a period or comma? 将根据字符串是否包含句点或逗号以外的特殊字符返回布尔值?

You can write: 你可以写:

string.matches("[a-zA-Z.? ]*")

That will evaluate to true if every character in the string is either a lowercase letter az , an uppercase letter AZ , a period, a question mark, or a space. 如果字符串中的每个字符都是小写字母az ,大写字母AZ ,句点,问号或空格,则结果为true (And to false otherwise, of course.) (当然,否则为false 。)

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

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