英文:
How can I find lines that do not end in a certain character(") and then add that character using find and replace in VS Code?
问题
我需要一个正则表达式,它能匹配不以特定字符(")结尾的行,并替换整行并在末尾添加该字符。使用正则表达式和VS Code的查找与替换功能,以下是匹配和替换的表达式:
匹配表达式:
[^\n"](\n|$)
替换表达式:
$0"
英文:
As the title says I need a Regex expression that will match a line that does not end in a certain character(") and then to substitute the entire line and append at end.
There are lines in my file that should end with " but it is missing so I need to find these lines and add the missing "
Using Regex and VS Code find & replace this expression
[^\n"/n](\n|$)
finds all lines of this sort but I cannot append as it gets rid of the last character of the string when trying to replace with
$1 "
答案1
得分: 3
将VS Code的查找和替换功能设置为正则表达式模式。在搜索框中输入.*[^"]$
,在替换框中输入$0"
。在替换框中的$0
代表了每次匹配的整个内容。
英文:
Put VS Code find-and-replace in regex mode. Put .*[^"]$
in the search field, and put $0"
in the replace field. "$0
" in the replace field represents the whole content that was matched for each match.
答案2
得分: 1
你错过了最后一个字符,因为你匹配它,但在替换中只使用了捕获组 $1
。
你可以匹配 [^"]$
并在替换中使用完整匹配,如 $0 "
。
英文:
You are missing the last character as you match it, but only use the capture group in the replacement with $1
You can match [^"]$
and use the full match in the replacement like $0 "
答案3
得分: 1
而不是搜索不以"
结尾的行,您可以搜索所有行的末尾,而不区分可选的"
:
- 搜索:
"?$
- 替换:
"
如果一行已经有双引号,则将其替换为双引号。如果一行没有双引号,则添加一个双引号。
注意:请勿尝试在regex101上执行此操作,因为以"
结尾的行将被匹配两次(在双引号位置和行尾位置),这只在VScode中起作用。
英文:
Instead of searching the lines that don't end with "
, you can search all end of the line without distinction with an optional "
:
- search:
"?$
- replacement:
"
if a line already has a double quote, this one is replaced with a double quote. If a line doesn't have a double quote, a double quote is added.
Notice: don't try to do that with regex101 because a line that ends with "
is matched twice (at the double quote position and at the end of line position), it only works in VScode.
答案4
得分: 1
你真的不需要选择任何东西。
查找:(?<!"|^\s*)$
替换:"
该正则仅检查行尾 $
前面是否没有 "
或者不是一个只包含空白字符的空行。
查看 regex101 演示
英文:
You don't need to select anything really.
Find: (?<!"|^\s*)$
Replace: "
The regex just checks that the end of the line $
is not preceded by a "
or
is not a blank line with only whitespace on it.
See regex101 demo
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论