我正在尝试使用notepad ++批量替换字符串,但似乎无法正确使用正则表达式。
以下是两行
(405, 1, 1, 'AWESOME', 1, 752, 1, 753, 1, '[{"id":1,"left":1,"right":714,"title":"Department"},{"id":405,"left":752,"right":753,"title":"CHANGE THIS TO AWESOME 1"}]'),
(406, 1, 1, 'MO IS AWESOME', 1, 754, 1, 755, 1, '[{"id":1,"left":1,"right":714,"title":"Department"},{"id":406,"left":754,"right":755,"title":"RANDOM STRING NEEDS CHANGING"}]'),
我正在尝试更改每行json中的第二个标题值,但下面的正则表达式匹配第一个标题到json的结尾
(title.*?)"(.*?)"\}\]
此正则表达式选择以下行
title":"Department"},{"id":405,"left":752,"right":753,"title":"CHANGE THIS TO AWESOME 1"}]
而不是
title":"CHANGE THIS TO AWESOME 1"}]
要更改的示例字符串是
CHANGE THIS TO AWESOME 1` and `RANDOM STRING NEEDS CHANGING
答案 0 :(得分:2)
title.+?"title":"\K[^"]+
REPLACEMENT
. matches newline
<强>解释强>
title : literally title (ie. the first one in the line)
.+? : 1 or more any character but newline, not greedy
"title":" : literally
\K : forget all we have seen until this position
[^"\r\n]+ : 1 or more any character that is not double quote or line break
给定示例的结果:
(405, 1, 1, 'AWESOME', 1, 752, 1, 753, 1, '[{"id":1,"left":1,"right":714,"title":"Department"},{"id":405,"left":752,"right":753,"title":"REPLACEMENT"}]'),
(406, 1, 1, 'MO IS AWESOME', 1, 754, 1, 755, 1, '[{"id":1,"left":1,"right":714,"title":"Department"},{"id":406,"left":754,"right":755,"title":"REPLACEMENT"}]'),
答案 1 :(得分:1)
答案 2 :(得分:1)
如果你想要替换标题,那么比整个正则表达式匹配的更重要的是第二个捕获组匹配的内容。在您的示例中,匹配是
Group 1. 74-79 `title`
Group 2. 80-161 `:"Department"},{"id":405,"left":752,"right":753,"title":"CHANGE THIS TO AWESOME 1`
问题在于懒惰的占有欲被向前评估。即,正则表达式在第一个title
开始匹配,并从那里尝试找到匹配的sortest字符串,但由于终止"\}\]
最佳解决方案取决于您的数据是否存在任何不正常现象。
您提供的示例的最简单解决方案可能是title":"([^"]*)"\}\]
,但只有在标题从不包含引号字符且标题始终是行中的最后一个属性时才有效
答案 3 :(得分:1)