正则表达式检查字符串中间是否有空格
我想验证字符是否为字母数字:
Regex aNum = Regex("[a-z][A-Z][0-9]");
我想添加可能有空格的选项,因此它将是一个两个单词的表达式:
Regex aNum = Regex("[a-z][A-Z][0-9]["\\s]");
但找不到正确的语法。
id 进行任何煽动。
I want to validate that the characters are alpha numeric:
Regex aNum = Regex("[a-z][A-Z][0-9]");
I want to add the option that there might be a white space, so it would be a two word expression:
Regex aNum = Regex("[a-z][A-Z][0-9]["\\s]");
but couldn't find the correct syntax.
id applicate any incite.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
[A-Za-z0-9\s]{1,}
应该适合你。它匹配任何包含字母数字或空白字符且长度至少为 1 个字符的字符串。如果您接受下划线,也可以将其缩短为[\w\s]{1,}
。您应该添加 ^ 和 $ 来验证整个字符串匹配,而不仅仅是字符串的一部分:
^[A-Za-z0-9\s]{1,}$
或^ [\w\s]{1,}$
。[A-Za-z0-9\s]{1,}
should work for you. It matches any string which contains alphanumeric or whitespace characters and is at least one char long. If you accept underscores, too you shorten it to[\w\s]{1,}
.You should add ^ and $ to verify the whole string matches and not only a part of the string:
^[A-Za-z0-9\s]{1,}$
or^[\w\s]{1,}$
.恰好两个带有单个空格的单词:
或任意数量的带有任意数量空格的单词:
Exactly two words with single space:
OR any number of words having any number of spaces:
匹配字母数字字符和空格。如果您想要一个可以包含空格但希望确保它以字母数字字符开头和结尾的单词,您可以尝试
matches alphanumeric characters and whitespace. If you want a word that can contain whitespace but want to ensure it starts and ends with an alphanumeric character you could try
为了不允许空字符串
并允许空字符串,
我添加了 ?? "" 因为 IsMatch 不接受空参数
To not allow empty strings then
and to allow empty strings
I added the ?? "" as IsMatch does not accept null arguments
如果您想检查字符串中间是否有空格,可以使用以下模式:
"(\w\s)+"
:这必须与至少带有空格的单词匹配。"(\w\s)+$"
:这必须匹配至少包含空格的单词,并且必须以空格结尾。"[\w\s]+"
:匹配单词或空格或两者。If you want to check for white space in middle of string you can use these patterns :
"(\w\s)+"
: this must match a word with a white space at least."(\w\s)+$"
: this must match a word with a white space at least and must finish with white space."[\w\s]+"
: this match for word or white space or the two.