VS Code 使用 Regex 跨文件查找和替换以更正特定行上出现的引号数量错误
VS Code Find and Replace across files using Regex to correct error in number of quotes appearing on a particular line
编辑:
虽然@Wiktor Stribiżew 的回答帮助我解决了我的两个问题(再次感谢!),但我遇到了第三个问题,与第二个问题相同。
错误:
"Image" : "'Motorola_About'
'Motorola_About' 话题前有一个不需要的单引号,结束引号需要改为双引号。冒号(:)前后有白色-space需要保留
我在上一期给出的答案行中尝试了以下代码
查找:
(" : ")"([^']*_About[^']")
("/s:/s")"([^']*_About[^']")
替换:
两种方法均无效。如果能告诉我哪里出错了,我将不胜感激。
轨迹:
我正在尝试使用 VS Code(版本 1.55.2)'Replace in Files' 来更正多个文件中特定行上出现的引号数量错误。
在这两种错误情况下,有一个额外的单引号 (') 出现在错误的相同位置(在图像名称之前),我需要在所有文件中删除它。
错误类型 1:
html file1 Line 50: 'ImageName':''Microsoft_About',
html file2 Line 51: 'ImageName':''Motorola_About',
html file3 Line 53: 'ImageName':''Apple_About',
..等等 450 个奇数文件
错误类型 2:
html file9 Line 50: "ImageName":"'Microsoft_About",
html file12 Line 51: "ImageName":"'Motorola_About",
html file15 Line 53: "ImageName":"'Apple_About",
..等等另外 350 个奇数文件
我尝试在 'Replace in Files' 中使用以下正则表达式代码:
对于错误 1:
FIND: ([''])([a-zA-Z0-9,.-]+_)(_About')
REPLACE: _About'
对于错误 2:
FIND: (["'])([a-zA-Z0-9,.-]+_)(_About')
REPLACE: _About'
它不起作用。我是 Regex 的新手,不熟悉这些代码,因此非常感谢对此的任何帮助和建议。
我使用一个正则表达式来匹配具有相同替换语句的两种情况。 Regex Explanation
const regex = /('|")(.*?)(?:'|"):(?:'|")+(.*?)(?:'|")/;
const error1 = [`'ImageName':''Apple_About'`, `'ImageName':''Motorola_About'`, `'ImageName':''Microsoft_About'`];
const error2 = [`"ImageName":"'Microsoft_About"`,`"ImageName":"'Motorola_About"`, `"ImageName":"'Apple_About"`];
console.log(error1.map(err => err.replace(regex, ':')));
console.log(error2.map(err => err.replace(regex, ':')));
您可以使用第一组文件
查找: '('[^']*)(_About')
替换:</code></p>
<p>见<a href="https://regex101.com/r/3fmsWs/2/" rel="nofollow noreferrer">regex demo</a>。对于第二个,您可以使用</p>
<p><em>查找</em>: <code>(":")'([^"]*_About")
替换:</code></p>
<p>见<a href="https://regex101.com/r/3fmsWs/3/" rel="nofollow noreferrer">this regex demo</a>。</p>
<p><em>详情</em>:</p>
<ul>
<li><code>'('[^']*)(_About')
- '
匹配,然后 '
和 '
以外的零个或多个字符被捕获到第 1 组(</code> ) 然后 <code>_About'
被捕获到第 2 组 (</code>)。</li>
<li><code>(":")'([^"]*_About")
- ":"
首先被捕获到第 1 组 (</code>),然后 <code>'
被匹配,然后 [= 以外的任何零个或多个字符=25=] 和 _About"
被捕获到第 2 组。
匹配被替换为适当的组值。
编辑:
虽然@Wiktor Stribiżew 的回答帮助我解决了我的两个问题(再次感谢!),但我遇到了第三个问题,与第二个问题相同。
错误:
"Image" : "'Motorola_About'
'Motorola_About' 话题前有一个不需要的单引号,结束引号需要改为双引号。冒号(:)前后有白色-space需要保留
我在上一期给出的答案行中尝试了以下代码 查找:
(" : ")"([^']*_About[^']")
("/s:/s")"([^']*_About[^']")
替换:
两种方法均无效。如果能告诉我哪里出错了,我将不胜感激。
轨迹: 我正在尝试使用 VS Code(版本 1.55.2)'Replace in Files' 来更正多个文件中特定行上出现的引号数量错误。 在这两种错误情况下,有一个额外的单引号 (') 出现在错误的相同位置(在图像名称之前),我需要在所有文件中删除它。
错误类型 1:
html file1 Line 50: 'ImageName':''Microsoft_About',
html file2 Line 51: 'ImageName':''Motorola_About',
html file3 Line 53: 'ImageName':''Apple_About',
..等等 450 个奇数文件
错误类型 2:
html file9 Line 50: "ImageName":"'Microsoft_About",
html file12 Line 51: "ImageName":"'Motorola_About",
html file15 Line 53: "ImageName":"'Apple_About",
..等等另外 350 个奇数文件
我尝试在 'Replace in Files' 中使用以下正则表达式代码: 对于错误 1:
FIND: ([''])([a-zA-Z0-9,.-]+_)(_About')
REPLACE: _About'
对于错误 2:
FIND: (["'])([a-zA-Z0-9,.-]+_)(_About')
REPLACE: _About'
它不起作用。我是 Regex 的新手,不熟悉这些代码,因此非常感谢对此的任何帮助和建议。
我使用一个正则表达式来匹配具有相同替换语句的两种情况。 Regex Explanation
const regex = /('|")(.*?)(?:'|"):(?:'|")+(.*?)(?:'|")/;
const error1 = [`'ImageName':''Apple_About'`, `'ImageName':''Motorola_About'`, `'ImageName':''Microsoft_About'`];
const error2 = [`"ImageName":"'Microsoft_About"`,`"ImageName":"'Motorola_About"`, `"ImageName":"'Apple_About"`];
console.log(error1.map(err => err.replace(regex, ':')));
console.log(error2.map(err => err.replace(regex, ':')));
您可以使用第一组文件
查找: '('[^']*)(_About')
替换:</code></p>
<p>见<a href="https://regex101.com/r/3fmsWs/2/" rel="nofollow noreferrer">regex demo</a>。对于第二个,您可以使用</p>
<p><em>查找</em>: <code>(":")'([^"]*_About")
替换:</code></p>
<p>见<a href="https://regex101.com/r/3fmsWs/3/" rel="nofollow noreferrer">this regex demo</a>。</p>
<p><em>详情</em>:</p>
<ul>
<li><code>'('[^']*)(_About')
- '
匹配,然后 '
和 '
以外的零个或多个字符被捕获到第 1 组(</code> ) 然后 <code>_About'
被捕获到第 2 组 (</code>)。</li>
<li><code>(":")'([^"]*_About")
- ":"
首先被捕获到第 1 组 (</code>),然后 <code>'
被匹配,然后 [= 以外的任何零个或多个字符=25=] 和 _About"
被捕获到第 2 组。
匹配被替换为适当的组值。