我正在寻找一种使用正则表达式查找和替换句子的方法。正则表达式应该能够找到任何长度的句子。我可以用 .* 得到整个句子,但这不允许它用 \1 替换。
FIND:
"QUESTION1" = "What is the day satellite called?"
"ANSWER1" = "The sun"
REPLACE:
<key>What is the day satellite called?</key>
<key>The sun</key>
使用以下表达式查找(需要修饰符:g
和m
):
^[^=]+= "(.*?)"$
然后使用以下方法替换它们:
<key>$1</key>
或者
<key>\1</key>
一种紧凑的方法:搜索(?m)"([^"]+)"$
<key>$1</key>
如果需要,请替换为<key>What is the day satellite called?</key>
或者
替换:"<key>$1</key>"
如果你愿意"<key>What is the day satellite called?</key>"
使用 perl 单行:
perl -pe 's!(?m)"([^"]+)"$!<key>$1</key>!g' yourfile
使用 perl:
> cat temp
"QUESTION1" = "What is the day satellite called?"
"ANSWER1" = "The sun"
> perl -lne 'print "<key>".$1."<\/key>" if(/\".*?\".*?\"(.*?)\"/)' temp
<key>What is the day satellite called?</key>
<key>The sun</key>
>
对于那些来自谷歌搜索的人,你可以在这里使用这个很棒的工具来找到正确的regex
表达方式: http ://regexr.com/