开发者

Mysql regex match names with a maximum of two words

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜