Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Replace 用lua中的正则表达式替换字符串_Replace_Lua_Lua Patterns - Fatal编程技术网

Replace 用lua中的正则表达式替换字符串

Replace 用lua中的正则表达式替换字符串,replace,lua,lua-patterns,Replace,Lua,Lua Patterns,我有很多日志文件,每个都包含1000多行。 文件的一部分如下: {@BLOCK|1%r1331|00 {@A-JUM|0|-9.352000E+06{@LIM2|+9.999999E+99|+1.000000E+04}} } {@BLOCK|1%x1001_swp|00 {@A-JUM|0|+3.362121E+00{@LIM2|+2.000000E+01|+0.000000E+00}} } {@BLOCK|1%x1101_swp|00 {@A-JUM|0|+3.282704E+00{@LIM2

我有很多日志文件,每个都包含1000多行。 文件的一部分如下:

{@BLOCK|1%r1331|00
{@A-JUM|0|-9.352000E+06{@LIM2|+9.999999E+99|+1.000000E+04}}
}
{@BLOCK|1%x1001_swp|00
{@A-JUM|0|+3.362121E+00{@LIM2|+2.000000E+01|+0.000000E+00}}
}
{@BLOCK|1%x1101_swp|00
{@A-JUM|0|+3.282704E+00{@LIM2|+2.000000E+01|+0.000000E+00}}
}
{@BLOCK|1%x201_swp|00
{@A-JUM|0|+3.276452E+00{@LIM2|+2.000000E+01|+0.000000E+00}}
}
{@BLOCK|1%x202_swp|00
{@A-JUM|0|+3.216571E+00{@LIM2|+2.000000E+01|+0.000000E+00}}
}
我想用另一个值替换“+3.282704E+00”(第8行)。重要的是要知道,像“{@BLOCK | 1%x1101_swp | 00”这样的每个标记都是唯一的,但是对于不同的文件,此标记的行号可能不同。 我试图在“@BLOCK”和“{@LIM2”之间的两行中使用正则表达式,但没有结果。 用于:

我试着:

if string.match(line,"{@BLOCK%|1%%1101_swp%|00..{@A-JUM%|0%|.............{@LIM2") then
string.gsub(line,"{@A-JUM%|0%|.............{@LIM2", "{@A-JUM%|0%|"..ff[#lines].."{@LIM2")
你可以用

local res = line:gsub("(%{@BLOCK%|1%%x201_swp%|00\r?\n%{@A%-JUM%|0%|).-(%{@LIM2%|)", "%1".. ff[$lines] .."%2")

详细信息

  • ({@BLOCK%| 1%%x201|swp%|00\r?\n%{@A%-JUM%|0%|)
    -第1组:
    {@BLOCK | 1%x201|swp | 00
    子字符串,后跟可选的CR符号,然后是LF,然后是
    {@A-JUM | 0 |
  • -
    -任何0+字符,尽可能少
  • ({@LIM2%|)
    -第2组:
    {@LIM2%|
    子字符串

%1
%2
占位符分别指的是从替换模式中存储在组1和组2中的值。

您可以使用这种更统一的方式:

local line = [[{@BLOCK|1%r1331|00
{@A-JUM|0|-9.352000E+06{@LIM2|+9.999999E+99|+1.000000E+04}}
}
{@BLOCK|1%x1001_swp|00
{@A-JUM|0|+3.362121E+00{@LIM2|+2.000000E+01|+0.000000E+00}}
}
{@BLOCK|1%x1101_swp|00
{@A-JUM|0|+3.282704E+00{@LIM2|+2.000000E+01|+0.000000E+00}}
}
{@BLOCK|1%x201_swp|00
{@A-JUM|0|+3.276452E+00{@LIM2|+2.000000E+01|+0.000000E+00}}
}
{@BLOCK|1%x202_swp|00
{@A-JUM|0|+3.216571E+00{@LIM2|+2.000000E+01|+0.000000E+00}}
}]]

local function Replace(line, unic_block, unic_num, to_num)
    unic_block = unic_block:gsub("%%","%%%%") -- escaping magic symbol '%'
    unic_num = unic_num:gsub("[%+%.]",function(c) return "%"..c end)  -- escaping magic symbols '+' and '.'
    return line:gsub("(" ..unic_block .. ".-%c+.-)" .. unic_num, "%1" .. to_num ) 
end
local xline = Replace(line, "{@BLOCK|1%x1101_swp|00", "+3.282704E+00", "9999999")

print (xline)

您是否也必须匹配它前面的行(
{@BLOCK | 1%x1101_swp | 00
)?是否必须精确匹配
+3.28270eE+00
?如果是,这很简单。请提供更多详细信息,然后我们可以更好地回答:)不,每个文件的值+3.28270eE+00都不同。但字符数相同(等于点数)。我希望尽可能不使用行号来标识此字段,仅使用正则表达式。
local line = [[{@BLOCK|1%r1331|00
{@A-JUM|0|-9.352000E+06{@LIM2|+9.999999E+99|+1.000000E+04}}
}
{@BLOCK|1%x1001_swp|00
{@A-JUM|0|+3.362121E+00{@LIM2|+2.000000E+01|+0.000000E+00}}
}
{@BLOCK|1%x1101_swp|00
{@A-JUM|0|+3.282704E+00{@LIM2|+2.000000E+01|+0.000000E+00}}
}
{@BLOCK|1%x201_swp|00
{@A-JUM|0|+3.276452E+00{@LIM2|+2.000000E+01|+0.000000E+00}}
}
{@BLOCK|1%x202_swp|00
{@A-JUM|0|+3.216571E+00{@LIM2|+2.000000E+01|+0.000000E+00}}
}]]

local function Replace(line, unic_block, unic_num, to_num)
    unic_block = unic_block:gsub("%%","%%%%") -- escaping magic symbol '%'
    unic_num = unic_num:gsub("[%+%.]",function(c) return "%"..c end)  -- escaping magic symbols '+' and '.'
    return line:gsub("(" ..unic_block .. ".-%c+.-)" .. unic_num, "%1" .. to_num ) 
end
local xline = Replace(line, "{@BLOCK|1%x1101_swp|00", "+3.282704E+00", "9999999")

print (xline)