sed替换正则表达式正确语法

sed替换正则表达式正确语法,sed,Sed,某物 我正在尝试将data name=“abc”abc替换为helloworld 然而,我的结果返回 data name=“helloworld”helloworld“abc” 我希望成为data name=“helloworld”尽管html文件应该由html适当的解析器解析,因为OP已经使用工具sed,所以使用awk添加此解决方案 awk -v str="helloworld" ' match($0,/"[^"]*/){ print substr(

某物

我正在尝试将
data name=“abc”
abc
替换为
helloworld

然而,我的结果返回

data name=“helloworld”helloworld“abc”


我希望成为
data name=“helloworld”

尽管html文件应该由html适当的解析器解析,因为OP已经使用工具
sed
,所以使用
awk
添加此解决方案

awk -v str="helloworld" '
match($0,/"[^"]*/){
  print substr($0,1,RSTART) str substr($0,RSTART+RLENGTH)
  next
}
1
' Input_file
使用
sed

sed 's/\(^[^"]*\)\("[^"]*"\)\(.*\)/\1"helloworld"\3/' Input_file

如果您想查找具有
数据名
字符串的行,则可以在上述解决方案中将
匹配($0,/“[^”]*/){
行更改为
/data name/&&match($0,/“[^”]*/){
s/
更改为
/data name/s

*
匹配零个或多个预分割字符。因此
“*
匹配零个或多个
字符。因此,
数据名称=“*”
匹配
数据名称=“
”(
“*
匹配零个字符)并将其替换

您似乎希望实际匹配
”中的文本。因此,将所有内容匹配到

请注意,最好使用支持xml的工具来编辑html。使用正则表达式纵横字谜学习正则表达式很有趣

sed 's/\(^[^"]*\)\("[^"]*"\)\(.*\)/\1"helloworld"\3/' Input_file
sed 's/data-name="[^"]*"/data-name="helloworld"/g'