1

I am trying to query a MYSQL database using regular expressions. I am looking to get the values under columns with this pattern 1234X32X12.

I have written some pseudo-code, but I am unfamiliar with regular expressions, so I was hoping someone could help me out.

CODE:

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
    }

Any help would be appreciated!

4 Answers 4

1

MySQL supports regex matching directly in its SQL commands, check out its manual:

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

(see this at sqlfiddle)

Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest to read into the appropriate section of the Mysql-Manual, which is very detailed on how to perform a regular expression search.

Comments

0

You can refer to mysql's regular expression match function.

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

Comments

0

IF its an X you are splitting on then this would do:

$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)

But I get the feeling X could be any alphabetical character, is it?

1 Comment

No X is the split. so the number 1234X32X12 is equivalent to survey_idXpage_idXquestion_id...thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.