简体   繁体   English

正则表达式:任何字符至少包含一个空格

[英]Regular expression: at least one blank space enclosed by any character

I'd like to force my users to have at least two words in a string divided by a blank space. 我想强迫我的用户在字符串中至少有两个单词除以空格。

Allowed is any string that has at least one blank space enclosed by at least one character to the left and to the right. 允许的是任何字符串,其左侧和右侧至少有一个字符至少包含一个空格。 No blank space on the front and no blank space at the end. 正面没有空白区域,末端没有空白区域。

Some examples: 一些例子:

  • John Doe (valid) John Doe(有效)
  • John Walter Doe (valid) John Walter Doe(有效)
  • John (not valid) 约翰(无效)
  • John $ole (valid) 约翰$ ole(有效)
  • 3d3n 1snt f4r (valid) 3d3n 1snt f4r(有效)
  • John the 3rd (valid) 约翰三世(有效)
  • Jöning Wölter (valid) JöningWölter(有效)
  • Joe Garçon (valid) JoeGarçon(有效)
  • Harald Spaß (valid) HaraldSpaß(有效)

Just FYI: I want to use this for a very weak full name check. 仅供参考:我想用它来进行非常弱的全名检查。 Since there are MANY special characters that can occur in names, I simply want my users to force to have at least two words in a string. 由于名称中可能出现许多特殊字符,因此我只希望我的用户强制在字符串中至少包含两个单词。

If I understand your requirement, it's translated like this in regex: 如果我理解你的要求,它在正则表达式中翻译成这样:

^\S+\s+.*\S$

If you want your white space to be just " ", then use 如果您希望您的空白区域只是“”,那么请使用

^\S+ +.*\S$

But be careful that you make cultural assumptions here. 但要小心你在这里做出文化假设。 Real names don't follow strict rules. 真实姓名不遵守严格的规定。

This matches a word followed by one or more groups of whitespace + word 这匹配一个单词后跟一个或多个空格+单词组

/^\S+(\s+\S+)+$/

It does match any whitespace though so perhaps 尽管如此,它确实匹配任何空白

/^\S+( +\S+)+$/

would be better 会更好

尝试这样的事情:

^(\S+)(\s{1}\S+)+$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM