Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用注释@RegExp进行Spring验证_Spring_Spring Mvc - Fatal编程技术网

使用注释@RegExp进行Spring验证

使用注释@RegExp进行Spring验证,spring,spring-mvc,Spring,Spring Mvc,Im使用Spring表单验证来验证用户输入的输入字段。我需要帮助在一个特定的领域包括空间。下面是我使用的验证注释。但它似乎不允许空间 @RegExp(value="([0-9|a-z|A-Z|_|$|.])*",message="value can contain only digits,alphabets or _ or . or $") private String cName ; 我想知道我需要在验证注释中包含什么值才能在名称中包含空格 我试图在exp值中包含“\s”以包含空格。但它似乎

Im使用Spring表单验证来验证用户输入的输入字段。我需要帮助在一个特定的领域包括空间。下面是我使用的验证注释。但它似乎不允许空间

@RegExp(value="([0-9|a-z|A-Z|_|$|.])*",message="value can contain only digits,alphabets or _ or . or $")
private String cName ;
我想知道我需要在验证注释中包含什么值才能在名称中包含空格

我试图在exp值中包含“\s”以包含空格。但它似乎不起作用


非常感谢您的帮助。

您的正则表达式字符串对您的要求无效

请改用以下正则表达式:

 //([0-9|a-z|A-Z|\\_|\\$|\\.|\\s])+

@Test
public void testRegex() {
    String r = "([0-9|a-z|A-Z|\\_|\\$|\\.|\\s])+";
    assertTrue("Allows space", Pattern.matches(r, "test test"));
    assertTrue("Allows .", Pattern.matches(r, "12My.test"));
    assertTrue("Allows _", Pattern.matches(r, "My_123"));
    assertTrue("Allows $", Pattern.matches(r, "$ 1.0"));

}

为什么字符类中的管道
|