Home > other >  Create regex except specific pattern
Create regex except specific pattern

Time:01-18

I have this example

CREATE TABLE `test_table` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `ref_id` bigint(20) unsigned NOT NULL COMMENT 'ref ID',
  `code` varchar(4) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'code',
  `name` varchar(40) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'name',
  `reg_ts` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'reg_ts',
  `upd_ts` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT 'upd_ts',
  PRIMARY KEY (`id`),
  KEY `idx_name` (`name`) COMMENT 'name index',
  KEY `idx_code` (`code`) COMMENT 'code index'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='test table'

I want to match remove following lines

KEY `idx_name` (`name`) COMMENT 'name index',
KEY `idx_code` (`code`) COMMENT 'code index'

So I tried to match line with regex, and replace with empty string. But I don't know how to match KEY * pattern without PRIMARY KEY * pattern. Anyone can help me?

I'm trying this job with SublimeText 3 find feature.
If I can possible, remove comma(,) after PRIMARY KEY.

CodePudding user response:

You may try the following find and replace, in multiline mode:

Find:    (?:^|(?<=\n))\s*KEY .*?\r?\n
Replace: (empty)

This should match every line which starts with the keyword KEY, it being possibly preceded by whitespace.

  • Related