开发者

Mysql regex match names with a maximum of two words

开发者 https://www.devze.com 2023-03-10 15:26 出处:网络
i\'ve being trying this without succe开发者_StackOverflow社区ss: select * from table where name regexp \'^[:alpha:]{2}$\'

i've being trying this without succe开发者_StackOverflow社区ss:

select * from table where name regexp '^[:alpha:]{2}$'

pls help me?


There probably needs to be some white space in between the two words, right? Try

select * from table where name regexp '^[[:alpha:]]+[[:space:]]*[[:alpha:]]*$'
  • [[:alpha:]]+ matches one or more letter characters
  • [[:space:]]* matches zero or more whitespace characters. (You may want to use [[:blank:]]* instead, to only match spaces and tabs, or [[ ]]* for spaces only.)
  • [[:alpha:]]* matches zero or more letter characters

So this should accept strings like

  • "foo"
  • "foo "
  • "foo "
  • "foo bar"
  • "foo bar"

and reject strings like

  • " foo"
  • " foo "
  • "foo bar baz"


select * from table where name regexp '^[:alpha:][:blank:]^[:alpha:]*$'
0

精彩评论

暂无评论...
验证码 换一张
取 消