1

我正在尝试使用正则表达式查询 MYSQL 数据库。我正在寻找具有此模式 1234X32X12 的列下的值。

我写了一些伪代码,但我不熟悉正则表达式,所以我希望有人能帮助我。

代码:

if ($transposed_array[$i][0] starts with number)
    {
        $sid = regex something /^[0-9]*/
        $gid = regex something /X[0-9]*X/
        $gid = remove first and last character from $gid
        $qid = regex something /[0-9]*$/

        $question_text = mysql_query("select question from table where sid = ".$sid." and gid = ".$gid." and qid = ".$qid." limit 1");
        $transposed_array[$i][0] = $question_test
    }

任何帮助,将不胜感激!

4

4 回答 4

1

MySQL 在其 SQL 命令中直接支持正则表达式匹配,请查看其手册

select col from table where col regexp '^h';

在 sqlfiddle 看到这个

于 2012-10-25T18:40:35.420 回答
0

我建议阅读Mysql-Manual的相应部分,该部分非常详细地介绍了如何执行正则表达式搜索。

于 2012-10-25T18:41:34.267 回答
0

可以参考mysql的正则表达式匹配函数。

http://dev.mysql.com/doc/refman/5.0/en/regexp.html

于 2012-10-25T18:42:00.490 回答
0

如果它是一个 X 你正在分裂,那么这会做:

$var = "1234X32X12";

$bits = explode("X", $var);

var_dump( $bits);

array
  0 => string '1234' (length=4)
  1 => string '32' (length=2)
  2 => string '12' (length=2)

但我觉得 X 可以是任何字母字符,是吗?

于 2012-10-25T18:45:00.657 回答