Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Split SQL INSERT statement

Last post 09-27-2007, 3:13 PM by ddrudik. 1 replies.
Sort Posts: Previous Next
  •  09-27-2007, 2:28 PM 35202

    Split SQL INSERT statement

    Hello

    I have to parse a SQL INSERT statement to get all the values to include in the database.

    I thought that a regex should exist to do this, and I found this very nice regex (http://regexlib.com/REDetails.aspx?regexp_id=1750) :

    (INSERT INTO\s+)(\w+)(\s+\()([\w+,?\s*]+)(\)\s+VALUES\s+)((\(['?\w+'?,?\s*]+\)\,?;?\s*)+)

     

    Unfortunately,  it doesn't work with values containing other characters than letters.

    For exemple this insert request does not match :  INSERT INTO my_table (id, file) VALUES (1, 'file.java');

    Could you tell me how change the regex to match with values containing other characters than letters (for exemple : ' , . - < > ( ) ) ?

     

    Thanks a lot for your help !

     

     

     

  •  09-27-2007, 3:13 PM 35203 in reply to 35202

    Re: Split SQL INSERT statement

    There's probably little chance that a regex will match any/all permutations of the SQL INSERT statement, but here's an option that matches your source text example:

    (INSERT INTO\s+)(\w+)(\s+\()((\w+,?\s*)+)(\)\s+VALUES\s+)\(((\s*'?\S+'?,?\s*)+)\)

    Resulting in:

      PHP Version = 5.2.4
      PCRE Library Version = 7.2 2007-06-19
      Array
      (
          [0] => Array
              (
                  [0] => INSERT INTO my_table (id, file) VALUES (1, 'file.java')
              )
      
          [1] => Array
              (
                  [0] => INSERT INTO 
              )
      
          [2] => Array
              (
                  [0] => my_table
              )
      
          [3] => Array
              (
                  [0] =>  (
              )
      
          [4] => Array
              (
                  [0] => id, file
              )
      
          [5] => Array
              (
                  [0] => file
              )
      
          Devil => Array
              (
                  [0] => ) VALUES 
              )
      
          [7] => Array
              (
                  [0] => 1, 'file.java'
              )
      
          Music => Array
              (
                  [0] => 'file.java'
              )
      
      )
      

View as RSS news feed in XML