正则表达式允许在Java中使用空格字符 [英] Regex allowing a space character in Java

查看:40
本文介绍了正则表达式允许在Java中使用空格字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,所以我试图允许一些文本输入在发送之前经过正则表达式检查.我希望文本仅包含A-Z,0-9和空格"字符.现在是我的代码:

Hey all, so I'm trying to allow some text input which goes through a regex check before it's sent off. I want the text to only include A-Z, 0-9, and the space " " character. Here is my code now:

if(!title.matches("[a-zA-Z0-9_]+") {
    //fail
}
else {
    //success
}

但是当我输入这是一个测试"之类的东西时,这仍然会给出//fail 结果

However this still gives the //fail result when I input something like "This is a test"

有什么想法吗?谢谢大家.

Any ideas? Thanks all.

推荐答案

您没有在正则表达式中包含空格.请尝试以下操作:

You're not including the space in the Regex. Try the following:

if (!title.matches("[a-zA-Z0-9 ]+"))

这篇关于正则表达式允许在Java中使用空格字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆