正则表达式替换IntelliJ中没有引号的字符串 [英] Regex to replace string not in quotes in IntelliJ
问题描述
我不太擅长正则表达式,但是我想做的是替换所有匹配项
I'm not very good at regex stuff, but what I'm trying to do is replace anything that matches
动作:某事
东西不在单引号或双引号之内.
where the something is not inside of either single or double quotes.
我想用动作"something"代替它.
I want to replace this with action: "something".
因此,它不应替换动作:某物"或动作:某物",因为它们已包含引号.东西可以是任何单词,所以它不是静态的.动作:块将始终是静态的.
So, it should not replace action: "something" or action: 'something' since those already contain quotes. The something could be any word, so it's not static. The action: piece will always be static.
推荐答案
使用捕获组来完成此任务:
Use capture groups to accomplish this:
action: (\w+)
并替换为:
action: "$1"
查看实际运行情况: http://regex101.com/r/sP4pP7
action: something
//becomes
action: "something"
很显然,如果您需要更复杂的内容(非单词字符),它将更改.您可以使用类似这样的内容:
Obviously if you want something more sophisticated (non-word characters) it changes. You could use something like this:
^action: ([.-\w]+)$
这将确保字符串以action:
开头(通过^
)和any word-character, period, or dash
直到字符串的末尾($
).
Which will ensure the string begins with action:
(via ^
), and any word-character, period, or dash
until the end of the string ($
).
这篇关于正则表达式替换IntelliJ中没有引号的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!