1

Hi everyone I'm trying to select the content in between the second and third comma using regex

This is my content

INSERT INTO table (column1, column2, column3) VALUES ('Alejandro', 'dislike', '', 20, 'otro nombre')
INSERT INTO table (column1, column2, column3) VALUES ('Jando', 'like', '', 30, 'wtf')

As you can see between second and third comma are just single quotes '' and I want to selected them using regex because I need to modify like 5000 lines in sublime text 3, I hope you can help me, I tried with no success ,(.*){2} I know I'm wrong, I have no experience with regex

Note: No all time will be single quotes ''

Professor Zoom
  • 339
  • 1
  • 4
  • 13

1 Answers1

1

Use

 \bVALUES\s(?:[^\n,]*,){2}\h*\K[^,\n]+

See proof

Explanation

\b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  VALUES                   'VALUES'
--------------------------------------------------------------------------------
  \s                       whitespace (\n, \r, \t, \f, and " ")

--------------------------------------------------------------------------------
  (?:                      group, but do not capture (2 times):
--------------------------------------------------------------------------------
    [^\n,]*                  any character except: '\n' (newline),
                             ',' (0 or more times (matching the most
                             amount possible))
--------------------------------------------------------------------------------
    ,                        ','
--------------------------------------------------------------------------------
  ){2}                     end of grouping
--------------------------------------------------------------------------------
  \h*                      horizontal whitespace (0 or more times 
                           (matching the most amount possible))
--------------------------------------------------------------------------------
  \K                       match reset operator (discarding text matched so far)
--------------------------------------------------------------------------------
  [^,\n]+                  any character except: ',', '\n' (newline)
                           (1 or more times (matching the most amount
                           possible))

Another attempt:

\bVALUES\s*\((?:\s*'(?:''|[^'])*'\s*,){2}\s*\K'(?:''|[^'])*'

See another proof

Explanation

--------------------------------------------------------------------------------
  \b                       the boundary between a word char (\w) and
                           something that is not a word char
--------------------------------------------------------------------------------
  VALUES                   'VALUES'
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \(                       '('
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (2 times):
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    '                        ' char
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
--------------------------------------------------------------------------------
      ''                       ''
--------------------------------------------------------------------------------
     |                        OR
--------------------------------------------------------------------------------
      [^']                     any character except: '
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
    '                        '\''
--------------------------------------------------------------------------------
    \s*                      whitespace (\n, \r, \t, \f, and " ") (0
                             or more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    ,                        ','
--------------------------------------------------------------------------------
  ){2}                     end of grouping
--------------------------------------------------------------------------------
  \s*                      whitespace (\n, \r, \t, \f, and " ") (0 or
                           more times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
  \K                       match reset operator (discarding text matched so far)
--------------------------------------------------------------------------------
  '                        '
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (0 or more times
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    ''                       '\'\''
--------------------------------------------------------------------------------
   |                        OR
--------------------------------------------------------------------------------
    [^']                     any character except: '''
--------------------------------------------------------------------------------
  )*                       end of grouping
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37