我想替换以下字符串
comments={ts=2010-02-09T04:05:20.777+0000, comment_id=529590|2886|LOL|Baoping Wu|529360}
in
comments={ts=2010-02-09T04:05:20.777+0000, comment_id=529590, user_id = 2886, comment='LOL', user= 'Baoping Wu', post_commented=529360}
我的方法是comment_id =。([0-9])*为第一次替换 对于其他替换我很难。任何人都可以帮助我吗?
答案 0 :(得分:0)
您可以通过一次搜索和替换操作执行所有这些转换。使用以下具有捕获组的正则表达式:
(comment_id=)(\d+)\|(\d+)\|([^|]+)\|([^|]+)\|(\d+)
替换为$1$2, user_id = $3, comment='$4', user= '$5', post_commented=$6
请参阅regex demo
(comment_id=)
- 第1组,文字字符序列(\d+)
- 第2组:一个或多个数字\|
- 文字管道符号(\d+)
- 第3组匹配另一部分数字\|
- 再次,管道([^|]+)
- 第4组捕获|
\|
- 再次,管道([^|]+) - Group 5 capturing one or more symbols other than
|`\|
- 另一根烟斗(\d+)
- 第6组匹配另一部分数字在替换字符串中,$n
是对已捕获组的反向引用。
答案 1 :(得分:0)