我正在读取文件,我想找到一个时间戳记值并将其替换为0,然后再将其输出到另一个文件。因此,我正在尝试搜索和替换此值TIME =“ 20180731.5622743” 对于文件中的类似内容 TIME =“ 00000000.0000000”
到目前为止,我有..
public Bind() {
return Excel.run(async (ctx) => {
ctx.workbook.bindings.addFromNamedItem("Field1", "Range", "Field1");
ctx.workbook.bindings.addFromNamedItem("Field2", "Range", "Field2");
ctx.workbook.bindings.addFromNamedItem("Field3", "Range", "Field3");
ctx.workbook.bindings.addFromNamedItem("Field4", "Range", "Field4");
ctx.workbook.bindings.addFromNamedItem("Field5", "Range", "Field5");
ctx.workbook.bindings.addFromNamedItem("Field6", "Range", "Field6");
ctx.workbook.bindings.addFromNamedItem("Field7", "Range", "Field7");
ctx.workbook.bindings.addFromNamedItem("Field8", "Range", "Field8");
ctx.workbook.bindings.addFromNamedItem("Field9", "Range", "Field9");
ctx.workbook.bindings.addFromNamedItem("Field10", "Range", "Field10");
return ctx.sync();
}).catch(ErrorHandler);
}
public Get() {
return Excel.run(async (ctx) => {
var field1 = Office.context.document.settings.get("Field1");
if (field1 === null) {
field1 = await this.SelectField1();
}
return $.post({
url: 'getUrl',
data: { id: field1 }
}).done(async (model) => {
ctx.workbook.bindings.getItem("Field1").getRange().values = [[model.Field1]];
ctx.workbook.bindings.getItem("Field2").getRange().values = [[model.Field2]];
ctx.workbook.bindings.getItem("Field3").getRange().values = [[model.Field3]];
ctx.workbook.bindings.getItem("Field4").getRange().values = [[model.Field4]];
ctx.workbook.bindings.getItem("Field5").getRange().values = [[model.Field5]];
ctx.workbook.bindings.getItem("Field6").getRange().values = [[model.Field6]];
ctx.workbook.bindings.getItem("Field7").getRange().values = [[model.Field7]];
ctx.workbook.bindings.getItem("Field8").getRange().values = [[model.Field8]];
ctx.workbook.bindings.getItem("Field9").getRange().values = [[model.Field9]];
ctx.workbook.bindings.getItem("Field10").getRange().values = [[model.Field10]];
// Sync to update sheet in Excel
await ctx.sync();
});
}).catch(ErrorHandler);
}
搜索和替换仅部分起作用,因为该行包含其他我不想替换的时间戳。如果我包含要搜索和替换的全部内容,则引号似乎阻碍了搜索和替换的成功。例如。(
open(INPUT, $path) or die $!;
open(OUTPUT, ">$susex_path") or die $!;
while(<INPUT>){
$line =~ s/([0-9]+8.[0-9]+7)/000000.0000000/g;
print OUTPUT $line;
}
}
close(INPUT);
close(OUTPUT);
非常感谢您的帮助
答案 0 :(得分:0)
尝试使用正则表达式:(?<=TIME=")\d{8}\.\d{7}(?=")
答案 1 :(得分:0)
尝试一下可能会更快
$line =~ s/TIME="[0-9]{8}\.[0-9]{7}"/TIME="00000000.0000000"/g
请注意,在问题注释中提到,您需要{8}和{7}而不是+8和+7,并且"
在s///
的任一侧都不是特殊的。
但是.
很特殊,需要转义;另外,除非您想在正则表达式之外使用原始时间戳,否则就不需要( ... )