今天碰到一个问题:
有一组整数用'|'隔开从小到大排列,现要确定某几个数是否在这个string写出mysql的正则表达式.
比如:
'|12|14|16|20|120|130|' 要确定12 16是否在这个string里面 .
最简单的办法是'.*|12|.*|16|.*',但是如果是'12' '14'呢?这个就不行了. '.*|12|.*|14|.*'是不会匹配前面的字符串的, 因为他们相邻,简单的解决办法是改造string用'||'分隔,但这样很浪费空间了.
看了半天manual发现一个很简单的方法'[[:<:]]12[[:>:]].*[[:<:]]14[[:>:]]' .
[[:<:]]是指匹配单词开始,[[:>:]]匹配单词尾部
manual上:
[[:<:]]
[[:>:]]
These markers stand for word boundaries. They match the beginning and end of words, respectively. A word is a sequence of word characters that is not preceded by or followed by word characters. A word character is an alphanumeric character in the alnum class or an underscore (_).
同时'曾经未来'给出了一个比较通用的方法.'.*|12|(.*|)*14|.*'
正则表达式的确非常神奇阿.这个也算一题多解吧.