简体   繁体   English

正则表达式正则表达式可验证输入:两个单词之间有一个空格

[英]Regular Expression regex to validate input: Two words with a space between

i need to use a regular expression to validate a field with php that has to have two words separated by a space like: "First Last" but i cant find one that fits my purposes, can anyone help me? 我需要使用正则表达式来验证php字段,该字段必须有两个单词,并用空格隔开,例如:“ First Last”,但我找不到适合我目的的单词,有人可以帮助我吗?

The best i've done is ^[a-zA-Z0-9_\\s]*$ but with this i can have more than one space and anywhere in the field and i want only between the words. 我做过的最好的事是^[a-zA-Z0-9_\\s]*$但是有了这个,我可以在田野中的任何地方有一个以上的空间,而我只想要单词之间。 Can anyone help me? 谁能帮我?

Something like ^\\w+\\s\\w+$ ought to work for this case. ^\\w+\\s\\w+$应该适用于这种情况。 But you don't necessarily need to use regular expressions for this, you could just use explode() . 但是您不必为此使用正则表达式,只需使用explode()

^[^\s]+\s[^\s]+$
  • [^\\s]+ matches one or more characters, except whitespace characters; [^\\s]+匹配一个或多个字符,空格字符除外;
  • \\s matches a single whitespace character. \\s匹配单个空格字符。

这可能会解决问题

^[a-zA-Z0-9]+ {1}[a-zA-Z0-9]+$

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

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