Regex 正则表达式查找和替换句子
我正在寻找一种使用正则表达式查找和替换句子的方法。正则表达式应该能够找到任意长度的句子。我可以用。*获取整个句子,但这不允许用\1替换它Regex 正则表达式查找和替换句子,regex,replace,find,sentence,Regex,Replace,Find,Sentence,我正在寻找一种使用正则表达式查找和替换句子的方法。正则表达式应该能够找到任意长度的句子。我可以用。*获取整个句子,但这不允许用\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> 查找:
FIND:
"QUESTION1" = "What is the day satellite called?"
"ANSWER1" = "The sun"
REPLACE:
<key>What is the day satellite called?</key>
<key>The sun</key>
查找:
“问题1”=“日间卫星叫什么?”
“答案1”=“太阳”
替换:
今天的卫星叫什么?
太阳
使用以下表达式查找(需要修饰符:g
和m
):
然后使用以下方法替换它们:
<key>$1</key>
1美元
或
\1
您需要使用捕获组。这样您就可以通过反向引用来引用捕获的组
正则表达式:
.*(?<= \")([^"]*).*
*(?使用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>
>
>cat温度
“问题1”=“日间卫星叫什么?”
“答案1”=“太阳”
>perl-lne“print”。$1.“if(/\”*?\“*?\”(.*?\”/)临时
今天的卫星叫什么?
太阳
>
Perl一行程序
一种紧凑的方法:搜索(?m)“([^”]+)”$
如果需要,请替换为$1
,当天卫星的名称是什么?
或
替换:“$1”
如果需要“日间卫星叫什么?”
使用perl单行程序:
perl -pe 's!(?m)"([^"]+)"$!<key>$1</key>!g' yourfile
perl-pe的!(?m)“([^”]+)“$!$1!g”文件
对于那些来自谷歌搜索的人,你可以在这里使用这个很棒的工具来找到正确的regex
表达式:你使用的是哪种语言对不起,我在BBEdit中使用了grep。谢谢Avinash!效果很好。我已经为此挣扎了一段时间,这比最初预期的要棘手。
<key>\1</key>
> 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>
>
perl -pe 's!(?m)"([^"]+)"$!<key>$1</key>!g' yourfile