SELECT以匹配特殊字符和大小写

时间:2015-04-28 12:47:31

标签: mysql regex select

中的字段 X 可能包含特殊字符,例如 hello!World ,我想知道是否有方法可以匹配使用 HelloWorld (忽略大小写和特殊字符) SELECT * FROM table WHERE X='Helloworld'

4 个答案:

答案 0 :(得分:1)

http://sqlfiddle.com/#!9/2afa1/1

如果你需要exaclty match of string:

SELECT * 
FROM table1
WHERE x REGEXP '^hello[[:punct:],[:space:]]world$';

如果hello world可能是更大字符串的一部分:

SELECT * 
FROM table1
WHERE x REGEXP 'hello[[:punct:],[:space:]]world';

答案 1 :(得分:0)

如果我理解你的问题,你需要过滤掉非ASCII字符吗?请确认这是否属实。为此,请查看评论链接和this问题中的REGEXP匹配。

尝试类似

的内容
SELECT * FROM `table ` WHERE `X` REGEXP 'Helloworld';

答案 2 :(得分:0)

您可以做的是替换所有特殊字符:

SELECT * FROM table WHERE LOWER(REPLACE(X, '!', '')) = LOWER('HelloWorld');

如果你需要更换更多,请将这些替换链接起来:

SELECT * FROM table WHERE LOWER(REPLACE(REPLACE(X, '!', ''), '?', '')) = LOWER('HelloWorld');

答案 3 :(得分:0)

1) cleanly compiles
2) has corrections to the output formatting
3) properly initializes values (good case for always initialize all values)
4) outputs the proper data
5) does not try to fiddle offset/indexes, etc
6) uses meaningful #define names, function parameter names, etc

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define NUM_DICE           (2)
#define NUM_ROLLS          (10000)
#define MIN_DICEROLL_VALUE (2)
#define MAX_DICEROLL_VALUE (12)

int diceRoll(int numDice);

int main(void)
{
    srand(time(NULL));

    int valuesOfRoll[MAX_DICEROLL_VALUE+1] = { 0 }; // notice the +1

    for(int i = 0; i < NUM_ROLLS; i++)
    {
        int index = diceRoll(NUM_DICE);
        valuesOfRoll[index]++;
    }

    for(int i = MIN_DICEROLL_VALUE; i <= MAX_DICEROLL_VALUE; i++)
    {
        // 2d for values 2...12  5d for values 0...10000  \n so each output on anew line
        printf("The number %2d was rolled %5d times\n", i, valuesOfRoll[i]);
    }
    return 0;
} // end function: main


int diceRoll(int numDice)
{
    int sum = 0;  // be sure to initialize value

    for(int i = 0; i < numDice; i++)
    {
        sum += (rand() % 6) + 1;
    }
    return sum;
} // end of function: diceRoll

注意:

  • 这会在其他东西的中间找到字符串;添加^和$以锚定到目的地。
  • 这假设非字母字符位于REGEXP 'hello[^[:alpha:]]*world' hello之间,而不是字符串中的其他位置。
  • 这依赖于相关的整理来做(或不做)案例折叠。