【发布时间】:2021-06-08 04:25:19
【问题描述】:
我目前正在为一个工作项目而苦苦挣扎。简而言之,我们有一个由软件自动生成的 300 万行长(因此无法手动编辑)的大文本文件。此文本文件描述了变量,其格式如下:
/begin var_type var_name
[content of variable]
/end var_type
但是,由于生产线上的一些混乱,一些变量被重复,这给最终用户带来了困扰。这就是我们所拥有的:
/begin var_type var_name //the original variable
[content of variable]
/end var_type
过了一会儿,我们有了
/begin var_type var_name_ext //same type, same name but with "_ext" at the end
[same content of variable]
/end var_type
我不是一个伟大的开发者,但我认为算法应该:
1: search for every name of variables_ext
2: check if they indeed have a "non _ext" counterpart"
-> if not (there is a var_name_ext but no non _ext counterpart), leave them alone
-> if yes, the algorithm does what the client wants, i.e:
a: delete original variable block (from /begin to /end)
b: delete "_ext" name extension in the name of the var_name_ext variable
我陷入困境的部分是,出于集成目的,这需要在 Ruby 中完成,这是一种我熟悉但并不真正精通的语言,因为我是初学者。我想我需要使用正则表达式,但我无法真正掌握在我的情况下实现它的方法。 当控制台输出看到 /begin 或 /end 标签时,我设法进行基本搜索,将“true”打印到控制台输出,但我真的坚持算法的实现。
感谢任何帮助/建议,谢谢!
【问题讨论】: