gpt4 book ai didi

regex - 正则表达式 :- String can contain any characters but should not be empty

转载 作者:行者123 更新时间:2023-12-02 13:49:16 26 4
gpt4 key购买 nike

我的要求是

"A string should not be blank or empty"

例如,字符串可以包含任意数量的字符或字符串,后跟任何特殊字符,但绝不能为空,例如,字符串可以包含“a,b,c”或“xyz123abc”或“12!@” $#%&*()9"或 "aa bb cc "

所以,这就是我尝试过的 空格或空格的正则表达式:-

 ^\s*$ 
^ is the beginning of string anchor
$ is the end of string anchor
\s is the whitespace character class
* is zero-or-more repetition of

我被困在如何否定正则表达式 ^\s*$ 以便它接受任何字符串,如 "a,b,c"或 "xyz"或 "12!@$#%&*()9"

感谢任何帮助。

最佳答案

不需要正则表达式。在 Groovy 中你有 isAllWhitespace方法:

groovy:000> "".allWhitespace       
===> true
groovy:000> " \t\n ".allWhitespace
===> true
groovy:000> "something".allWhitespace
===> false

所以问 !yourString.allWhitespace 应该告诉你你的字符串是空的还是空白的:)

关于regex - 正则表达式 :- String can contain any characters but should not be empty,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13403659/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com