正则分组和捕获
():用于分组和捕获子表达式。
大白话就是()匹配到的数据,通过美元符号加下标可以获取该数据,例如$1、$2, 下标从1开始。
下面的案例就采用该模式处理文本数据
有一份报表的数据,需要把数据入库到mysql数据库, 因为数据很多,不可能手动一个一个转,这里采用正则表达式,巧妙的转化为对应的sql;
报表数据如下:
转换为sql格式如下:
update menu set content_value = "[{\"label\":\"年订阅\",\"value\":1},{\"label\":\"月订阅\",\"value\":2}]" where id = 17;
因为id这一列在sql会作为where参数,这里调整下位置,放到后面一列
里面的数据包含双引号,sql语句特殊处理,需要采用正则表达式做字符串转义。
复制excel表格的文本到notepad++,数据如下:
- [{"label":"苹果","value":1},{"label":"西瓜","value":2}] 17
- [{"label":"苹果","value":1},{"label":"西瓜","value":2}] 19
- [{"label":"苹果","value":1},{"label":"西瓜","value":2}] 21
- [{"label":"没吃过","value":1},{"label":"已经吃过了","value":2}] 22
- [{"label":"空","value":""},{"label":"免费","value":"免费"},{"label":"收费","value":"收费"}] 28
- [{"label":"空","value":""},{"label":"免费","value":"免费"},{"label":"收费","value":"收费"}] 30
- [{"label":"空","value":""},{"label":"免费","value":"免费"},{"label":"收费","value":"收费"}] 32
- [{"label":"空","value":""},{"label":"免费","value":"免费"},{"label":"收费","value":"收费"}] 34
- [{"label":"模式1","value":0},{"label":"模式2","value":1},{"label":"模式3","value":2}] 67
- [{"label":"模式1","value":0},{"label":"模式2","value":1},{"label":"模式3","value":2}] 68
- [{"label":"打开","value":true},{"label":"关闭","value":false}] 69
- [{"label":"打开","value":true},{"label":"关闭","value":false}] 72
查找目标:"
替换为:\\\"
先标记
替换
双引号已接上反斜杠, 效果如下:
- [{\"label\":\"苹果\",\"value\":1},{\"label\":\"西瓜\",\"value\":2}] 17
- [{\"label\":\"苹果\",\"value\":1},{\"label\":\"西瓜\",\"value\":2}] 19
- [{\"label\":\"苹果\",\"value\":1},{\"label\":\"西瓜\",\"value\":2}] 21
- [{\"label\":\"没吃过\",\"value\":1},{\"label\":\"已经吃过了\",\"value\":2}] 22
- [{\"label\":\"空\",\"value\":\"\"},{\"label\":\"免费\",\"value\":\"免费\"},{\"label\":\"收费\",\"value\":\"收费\"}] 28
- [{\"label\":\"空\",\"value\":\"\"},{\"label\":\"免费\",\"value\":\"免费\"},{\"label\":\"收费\",\"value\":\"收费\"}] 30
- [{\"label\":\"空\",\"value\":\"\"},{\"label\":\"免费\",\"value\":\"免费\"},{\"label\":\"收费\",\"value\":\"收费\"}] 32
- [{\"label\":\"空\",\"value\":\"\"},{\"label\":\"免费\",\"value\":\"免费\"},{\"label\":\"收费\",\"value\":\"收费\"}] 34
- [{\"label\":\"模式1\",\"value\":0},{\"label\":\"模式2\",\"value\":1},{\"label\":\"模式3\",\"value\":2}] 67
- [{\"label\":\"模式1\",\"value\":0},{\"label\":\"模式2\",\"value\":1},{\"label\":\"模式3\",\"value\":2}] 68
- [{\"label\":\"打开\",\"value\":true},{\"label\":\"关闭\",\"value\":false}] 69
- [{\"label\":\"打开\",\"value\":true},{\"label\":\"关闭\",\"value\":false}] 72
查找目标:(\[\{)
替换为:update menu set content_value = "$1
已拼接上前缀,效果如下:
- update menu set content_value = "[{\"label\":\"苹果\",\"value\":1},{\"label\":\"西瓜\",\"value\":2}] 17
- update menu set content_value = "[{\"label\":\"苹果\",\"value\":1},{\"label\":\"西瓜\",\"value\":2}] 19
- update menu set content_value = "[{\"label\":\"苹果\",\"value\":1},{\"label\":\"西瓜\",\"value\":2}] 21
- update menu set content_value = "[{\"label\":\"没吃过\",\"value\":1},{\"label\":\"已经吃过了\",\"value\":2}] 22
- update menu set content_value = "[{\"label\":\"空\",\"value\":\"\"},{\"label\":\"免费\",\"value\":\"免费\"},{\"label\":\"收费\",\"value\":\"收费\"}] 28
- update menu set content_value = "[{\"label\":\"空\",\"value\":\"\"},{\"label\":\"免费\",\"value\":\"免费\"},{\"label\":\"收费\",\"value\":\"收费\"}] 30
- update menu set content_value = "[{\"label\":\"空\",\"value\":\"\"},{\"label\":\"免费\",\"value\":\"免费\"},{\"label\":\"收费\",\"value\":\"收费\"}] 32
- update menu set content_value = "[{\"label\":\"空\",\"value\":\"\"},{\"label\":\"免费\",\"value\":\"免费\"},{\"label\":\"收费\",\"value\":\"收费\"}] 34
- update menu set content_value = "[{\"label\":\"模式1\",\"value\":0},{\"label\":\"模式2\",\"value\":1},{\"label\":\"模式3\",\"value\":2}] 67
- update menu set content_value = "[{\"label\":\"模式1\",\"value\":0},{\"label\":\"模式2\",\"value\":1},{\"label\":\"模式3\",\"value\":2}] 68
- update menu set content_value = "[{\"label\":\"打开\",\"value\":true},{\"label\":\"关闭\",\"value\":false}] 69
- update menu set content_value = "[{\"label\":\"打开\",\"value\":true},{\"label\":\"关闭\",\"value\":false}] 72
查找目标:(\}\])\s{2}(\d{2})
替换为:$1\" where id = $2;\r
效果如下:
- update menu set content_value = "[{\"label\":\"苹果\",\"value\":1},{\"label\":\"西瓜\",\"value\":2}]" where id = 17;
-
- update menu set content_value = "[{\"label\":\"苹果\",\"value\":1},{\"label\":\"西瓜\",\"value\":2}]" where id = 19;
-
- update menu set content_value = "[{\"label\":\"苹果\",\"value\":1},{\"label\":\"西瓜\",\"value\":2}]" where id = 21;
-
- update menu set content_value = "[{\"label\":\"没吃过\",\"value\":1},{\"label\":\"已经吃过了\",\"value\":2}]" where id = 22;
-
- update menu set content_value = "[{\"label\":\"空\",\"value\":\"\"},{\"label\":\"免费\",\"value\":\"免费\"},{\"label\":\"收费\",\"value\":\"收费\"}]" where id = 28;
-
- update menu set content_value = "[{\"label\":\"空\",\"value\":\"\"},{\"label\":\"免费\",\"value\":\"免费\"},{\"label\":\"收费\",\"value\":\"收费\"}]" where id = 30;
-
- update menu set content_value = "[{\"label\":\"空\",\"value\":\"\"},{\"label\":\"免费\",\"value\":\"免费\"},{\"label\":\"收费\",\"value\":\"收费\"}]" where id = 32;
-
- update menu set content_value = "[{\"label\":\"空\",\"value\":\"\"},{\"label\":\"免费\",\"value\":\"免费\"},{\"label\":\"收费\",\"value\":\"收费\"}]" where id = 34;
-
- update menu set content_value = "[{\"label\":\"模式1\",\"value\":0},{\"label\":\"模式2\",\"value\":1},{\"label\":\"模式3\",\"value\":2}]" where id = 67;
-
- update menu set content_value = "[{\"label\":\"模式1\",\"value\":0},{\"label\":\"模式2\",\"value\":1},{\"label\":\"模式3\",\"value\":2}]" where id = 68;
-
- update menu set content_value = "[{\"label\":\"打开\",\"value\":true},{\"label\":\"关闭\",\"value\":false}]" where id = 69;
-
- update menu set content_value = "[{\"label\":\"打开\",\"value\":true},{\"label\":\"关闭\",\"value\":false}]" where id = 72;