Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Regular Expressions
  4. Trying to match non-quoted sections

Trying to match non-quoted sections

Scheduled Pinned Locked Moved Regular Expressions
databasetutorialmysqlsql-serversysadmin
12 Posts 3 Posters 32 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PIEBALDconsult
    wrote on last edited by
    #1

    Given the name of an SQL table, I am looking to add quotes as necessary (with an loose definition of "necessary"). With SQL Server, the parts of the name should be wrapped in brackets ([ and ]), MySQL uses backticks (`). So, for example:

    database.schema.table
    database.schema.[table]
    database.[schema].table
    [database].[schema].table

    should all be transformed to:

    [database].[schema].[table]

    What I have working; is capturing Wrapped and Unwrapped sections separately, wrapping the Unwrapped sections, and joining the sections back together. But it occurred to me that if I could Match only the Unwrapped sections, I could use Replace. However, I have so far been unsuccessful in my attempts (otherwise I wouldn't be posting). Does anyone out there have an idea of how to do this? I'm thinking it may involve Balancing Groups, but I've never used them before so I'm finding them confusing. This is not urgent. :-D Edit: I must have been over-thinking it :doh: . What I have now is (?<=^|\.)[^\[\]\.]+(?=$|\.)

    A 1 Reply Last reply
    0
    • P PIEBALDconsult

      Given the name of an SQL table, I am looking to add quotes as necessary (with an loose definition of "necessary"). With SQL Server, the parts of the name should be wrapped in brackets ([ and ]), MySQL uses backticks (`). So, for example:

      database.schema.table
      database.schema.[table]
      database.[schema].table
      [database].[schema].table

      should all be transformed to:

      [database].[schema].[table]

      What I have working; is capturing Wrapped and Unwrapped sections separately, wrapping the Unwrapped sections, and joining the sections back together. But it occurred to me that if I could Match only the Unwrapped sections, I could use Replace. However, I have so far been unsuccessful in my attempts (otherwise I wouldn't be posting). Does anyone out there have an idea of how to do this? I'm thinking it may involve Balancing Groups, but I've never used them before so I'm finding them confusing. This is not urgent. :-D Edit: I must have been over-thinking it :doh: . What I have now is (?<=^|\.)[^\[\]\.]+(?=$|\.)

      A Offline
      A Offline
      Andreas Gieriet
      wrote on last edited by
      #2

      Are you looking for this?

      string sql = @"database.schema.table
      database.schema.[table]
      database.[schema].table
      [database].[schema].table

      should all be transformed to:

      [database].[schema].[table]
      ";
      string pattern = @"(?:\[?(\w+)\]?)?\.\[?(\w+)\]?";
      Func replace = m =>
      (m.Groups[1].Success ? "[" + m.Groups[1].Value + "]" : "") + ".[" + m.Groups[2].Value + "]";
      Console.WriteLine("{0}", Regex.Replace(sql, pattern, m=>replace(m)));

      P L 2 Replies Last reply
      0
      • A Andreas Gieriet

        Are you looking for this?

        string sql = @"database.schema.table
        database.schema.[table]
        database.[schema].table
        [database].[schema].table

        should all be transformed to:

        [database].[schema].[table]
        ";
        string pattern = @"(?:\[?(\w+)\]?)?\.\[?(\w+)\]?";
        Func replace = m =>
        (m.Groups[1].Success ? "[" + m.Groups[1].Value + "]" : "") + ".[" + m.Groups[2].Value + "]";
        Console.WriteLine("{0}", Regex.Replace(sql, pattern, m=>replace(m)));

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        Does that work for names that contain SPACEs? my database.my schema.my table And Excel worksheet names that include a dollar sign ($) at the end? (I realize those were not listed in the original spec.)

        A 1 Reply Last reply
        0
        • A Andreas Gieriet

          Are you looking for this?

          string sql = @"database.schema.table
          database.schema.[table]
          database.[schema].table
          [database].[schema].table

          should all be transformed to:

          [database].[schema].[table]
          ";
          string pattern = @"(?:\[?(\w+)\]?)?\.\[?(\w+)\]?";
          Func replace = m =>
          (m.Groups[1].Success ? "[" + m.Groups[1].Value + "]" : "") + ".[" + m.Groups[2].Value + "]";
          Console.WriteLine("{0}", Regex.Replace(sql, pattern, m=>replace(m)));

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Wait till Smitha tackles that post! :laugh:

          Luc Pattyn [My Articles] Nil Volentibus Arduum

          A 1 Reply Last reply
          0
          • L Luc Pattyn

            Wait till Smitha tackles that post! :laugh:

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            A Offline
            A Offline
            Andreas Gieriet
            wrote on last edited by
            #5

            Hi Luc, Aaaah! You read my whine... ;-) I was quite upset - but I cooled down again :-) Cheers Andi [Edit] PS: ...and the tip is reverted to the "original" state again... [/Edit]

            1 Reply Last reply
            0
            • P PIEBALDconsult

              Does that work for names that contain SPACEs? my database.my schema.my table And Excel worksheet names that include a dollar sign ($) at the end? (I realize those were not listed in the original spec.)

              A Offline
              A Offline
              Andreas Gieriet
              wrote on last edited by
              #6

              You will run into problems here. The problem arises with spaces in the name, since the following pattern: ...([\w\s]+)... matches delete my database as well as delete database In this case I guess you don't get away without a parser (use the Regex for tokenizing, use the parser to detect all commands and translate the arguments where needed). Any names without spaces get easily translated, though, e.g.:

              string pattern = @"(?:\[?([\w\$]+)\]?)?\.\[?([\w\$]+)\]?";

              And if you have optional spaces around "[" and ".", the following a bit more complicted regex will do:

              ...
              string open = @"(?:\[\s*?)?";
              string close = @"(?:\s*?\])?";
              string ident = @"([\w\$]+)";
              string prefix = @"(?:" + open + ident + close + @"\s*?)?";
              string suffix = @"(?:" + open + ident + close + @")";
              string pattern = prefix + @"\.\s*?" + suffix;
              ...

              P 1 Reply Last reply
              0
              • A Andreas Gieriet

                You will run into problems here. The problem arises with spaces in the name, since the following pattern: ...([\w\s]+)... matches delete my database as well as delete database In this case I guess you don't get away without a parser (use the Regex for tokenizing, use the parser to detect all commands and translate the arguments where needed). Any names without spaces get easily translated, though, e.g.:

                string pattern = @"(?:\[?([\w\$]+)\]?)?\.\[?([\w\$]+)\]?";

                And if you have optional spaces around "[" and ".", the following a bit more complicted regex will do:

                ...
                string open = @"(?:\[\s*?)?";
                string close = @"(?:\s*?\])?";
                string ident = @"([\w\$]+)";
                string prefix = @"(?:" + open + ident + close + @"\s*?)?";
                string suffix = @"(?:" + open + ident + close + @")";
                string pattern = prefix + @"\.\s*?" + suffix;
                ...

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #7

                Andreas Gieriet wrote:

                matches
                 
                delete my database
                 
                as well as
                 
                delete database

                I expect the string to contain only the database, schema, and table names.

                A 1 Reply Last reply
                0
                • P PIEBALDconsult

                  Andreas Gieriet wrote:

                  matches
                   
                  delete my database
                   
                  as well as
                   
                  delete database

                  I expect the string to contain only the database, schema, and table names.

                  A Offline
                  A Offline
                  Andreas Gieriet
                  wrote on last edited by
                  #8

                  The Regex sees a line like aaa bbb ccc . ddd . eee fff What part of aaa bbb ccc is the database name? Only ccc or bbb ccc, etc.? You see the problem? The same for eee fff. Non-escaped/non-wrapped spaces in names is guess work to make them wrapped into [...]. I.e. to get from aaa bbb ccc .... to aaa [bbb ccc] .... is rather difficult, unless you know what aaa means or you say from outside that bbb ccc is a single name. Quite a challenge. ;-) Cheers Andi

                  P 1 Reply Last reply
                  0
                  • A Andreas Gieriet

                    The Regex sees a line like aaa bbb ccc . ddd . eee fff What part of aaa bbb ccc is the database name? Only ccc or bbb ccc, etc.? You see the problem? The same for eee fff. Non-escaped/non-wrapped spaces in names is guess work to make them wrapped into [...]. I.e. to get from aaa bbb ccc .... to aaa [bbb ccc] .... is rather difficult, unless you know what aaa means or you say from outside that bbb ccc is a single name. Quite a challenge. ;-) Cheers Andi

                    P Offline
                    P Offline
                    PIEBALDconsult
                    wrote on last edited by
                    #9

                    That should result in [aaa bbb ccc ].[ ddd ].[ eee fff]

                    A 1 Reply Last reply
                    0
                    • P PIEBALDconsult

                      That should result in [aaa bbb ccc ].[ ddd ].[ eee fff]

                      A Offline
                      A Offline
                      Andreas Gieriet
                      wrote on last edited by
                      #10

                      The line aaa bbb ccc.ddd eee ... could be ALTER TABLE dbo.tVersion ADD ... which in your approach would result in [ALTER TABLE dbo].[tVersion ADD] ... Forget about spaces or get as input the individual names (db name, table name, etc.) or make a parser that detects all language constructs and their db, table, etc. positions... I still think it's not worth the effort with names that contain spaces - too fragile. Cheers Andi

                      P 1 Reply Last reply
                      0
                      • A Andreas Gieriet

                        The line aaa bbb ccc.ddd eee ... could be ALTER TABLE dbo.tVersion ADD ... which in your approach would result in [ALTER TABLE dbo].[tVersion ADD] ... Forget about spaces or get as input the individual names (db name, table name, etc.) or make a parser that detects all language constructs and their db, table, etc. positions... I still think it's not worth the effort with names that contain spaces - too fragile. Cheers Andi

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #11

                        No, the string contains only the database, schema, and table name separated by periods as per the original post.

                        A 1 Reply Last reply
                        0
                        • P PIEBALDconsult

                          No, the string contains only the database, schema, and table name separated by periods as per the original post.

                          A Offline
                          A Offline
                          Andreas Gieriet
                          wrote on last edited by
                          #12

                          I was confused since I understood (say: assumed...) that you have an SQL script that you want to patch... Never assume anything ;-) In that case your initial regex is probably the simplest solution. Cheers Andi

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups