Regex allowing a space character in Java

后端 未结 2 1558
长发绾君心
长发绾君心 2020-12-11 03:14

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 \" \" charac

相关标签:
2条回答
  • 2020-12-11 03:26

    \s allows for any ASCII whitespace character. Consider using that rather than " ".

    if(!title.matches("[a-zA-Z0-9_\s]+")) {
        //fail
    }
    else {
        //success
    }
    
    0 讨论(0)
  • 2020-12-11 03:51

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

    if (!title.matches("[a-zA-Z0-9 ]+"))
    
    0 讨论(0)
提交回复
热议问题