【发布时间】:2020-12-17 09:45:45
【问题描述】:
删除整行的编码很容易,但我想弄清楚如何在 reg 表达式之间删除。我写了一个更有趣的例子,而不是我正在处理的技术报告:
当前文本:
cactus in the desert blooms @year round
and almost all cactus plants have very sharp needles
------ that may hurt you if you get too close-----
so stay away from the needles all @year and admire the
many colors in the buds @and flowers
more data more data more data
cactus in the desert blooms @year round
and almost all cactus plants have very sharp needles
so stay away from the needles all @year and admire the
data more data more data @year
data more data more data more data @year
many colors in the buds @and flowers
more data
cactus in the desert blooms @year round
and almost all cactus plants have very sharp needles
so stay away from the needles all @year and admire the
many colors in the buds @and flowers
more data more data
more data more data
more data more data
在这种情况下,我使用@year 作为初始reg 表达式,@and 作为第二个。我想删除 reg 表达式之间的删除部分和完整行。更喜欢使用 SED。
注意:@year 的任何后续实例都需要被忽略,直到找到 @and。如果找到多个@and 实例,则不应执行任何操作,因为之前没有@year 实例。
显示的所有示例都需要结果:
cactus in the desert blooms and flowers
more data more data more data
cactus in the desert blooms and flowers
more data more data more data
cactus in the desert blooms and flowers
more data
cactus in the desert blooms and flowers
more data more data
more data more data
more data more data
【问题讨论】:
-
能否让我们知道您是否可以多次出现
@and?如果是,那么您是否要匹配到它的最后一次出现或它的任何特定实例?或者您的实际 Input_file 中是否只有这些确切的行,如显示的样本本身?请确认一次。 -
您的示例和文本要求中缺少此类问题的所有常见用例,例如如何在
@and之前处理2个@years,在一个@year之后处理2个@ands,一个@and前面没有@year,一个@year没有后续@and,等等您将获得针对您展示的一个晴天案例的解决方案 - 您的输入中始终有一个@year和@and对。如果这不是您所需要的,那么 edit 您的问题会澄清您的要求并提供更实际的示例输入/输出。 -
您还得到了假设
@不能出现在您输入的任何其他位置的解决方案,因为它没有出现在您发布的示例中。如果这是一个错误的假设,那么edit 您的示例再次包含更多@s(以及@year或@and可以作为子字符串出现的情况,例如joe@andoverplumbers.com等) -
该文件包含自然出现的
@符号,但我使用 SED 将这些符号替换为空间。我将交换@year和@and。正则表达式@and肯定是一致的。但是,由于我必须进行交换以插入@year,因此在@and出现在文件中之前,它可能会在不同的行上出现多次。所以首先找到@year时需要打开搜索,找到@and后关闭搜索。我会将其添加到我的示例中。 -
如果您问题中的示例未涵盖您的所有用例,请编辑您的问题以显示更真实的示例。另外,不要在 cmets 中添加文本要求 - edit 您的问题要包含所有相关信息。