Match the first word then second letter of a string

How would you write a MySQL regex pattern that matches the full first word and the first character of the second word in a string. The string does not actually have to begin with a word. Case does not matter.

When searching for a string in which the first word occurrence is "Stack" and the next word begins with "O", the regex pattern should return true for all of the following:

  • -> Stack (Overflow) is great
  • Stack -> Overflow is great
  • Stack Overflow is great
  • and will fail if "Stack" is not the first word occurrence or the second word does not begin with an "O", like for the following:

  • Is Stack Overflow the best?
  • Stack is Overflow
  • I have tried some patterns but only came up with the following:

    SELECT * FROM members WHERE description REGEX '^[[:<:]]Stack[[:>:]].+[[:<:]]O'
    

    The problem here is:

  • It only matches strings that actually start with "Stack" but it should also match also strings like +=,Stack
  • It matches anything between and even words between "Stack" and "Overflow"
  • It will work if I can find the first and second word boundary.


    我认为这个表达可以做你想做的事情:

     where description rlike '^[^a-zA-Z]*Stack [^a-zA-Z]*O'
    
    链接地址: http://www.djcxy.com/p/75348.html

    上一篇: 找到匹配的词

    下一篇: 匹配字符串的第一个字,然后匹配第二个字母