将多行字符串与 perl regex 匹配并进行替换 [英] matching multiple line string with perl regex and doing a replace

查看:39
本文介绍了将多行字符串与 perl regex 匹配并进行替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个源文件,我在其中使用宏进行日志记录.典型的日志记录行如下所示:

I have a source file where I use a macro to do logging. Typical logging lines look like:

STDOUT_LOG(logDEBUG4) << "a single line log text" << aVariable;

还存在一些多行日志命令:

the exist some multiline log commands as well:

        STDOUT_LOG(logDEBUG4)
            << bsd->sensorName
            << " trainMinValueInWin = " << value.trough
            << " trainMaxValueInWin = " << value.peak
            << " |";

这个想法是抓取日志字符串(无论是单行还是多行)并将它们放在宏的括号中.

The idea is to grab the log string (whether it is in single line or multiple lines) and place them in the parenthesis of the macro.

通常,最终结果类似于:

Typically the end result would be something like:

 STDOUT_LOG(logDEBUG4,"a single line log text" << aVariable);

        STDOUT_LOG(logDEBUG4,
            bsd->sensorName
            << " trainMinValueInWin = " << value.trough
            << " trainMaxValueInWin = " << value.peak
            << " |");

我正在尝试阅读与多行匹配相关的任何内容,包括 perl 中的 while 循环、/s amd /m 运算符以及潜在的搜索和替换方法,但我离正确匹配多行宏还很远.我假设我需要/s 运算符才能使用."也匹配换行符,但我一直无法成功使用它.

I am trying to read up on anything related to multiline matching, including while loops in perl, the /s amd /m operators and potential ways to search and replace, but I am very far from correctly matching the multiline macro. I assume I need the /s operator in order for the "." to match newline characters too, but I have not been able to successfully use it.

模式的开头当然是STDOUT_LOG",结尾是第一次出现的;"无论多行之后它都会出现.

The beginning of the pattern is of course "STDOUT_LOG" and the end is the first occurrence of ";" however many lines later it shows up.

这次尝试

 perl -ne 's/STDOUT_LOG\((.*?)\)(.*?)\;/STDOUT_LOG($1,$2)\;/gs; print;'

设法获取日志级别(在这种情况下为logDEBUG4")对于单行日志命令来说是正常的",但是即使我最后使用了/s ,其余行的匹配也会失败.

manages to pick up the log level ("logDEBUG4" in this case) is "sort" of OK for single line log commands, but matching of the rest of the lines fails even though I used the /s in the end.

我该如何解决这个问题?我应该使用哪些其他运算符?有什么好的教程吗?最终有一个可以挽救这一天的 perl 命令吗?

How do I approach this problem? what other operators should I use? any good tutorials out there? ultimately is there a perl command that can save the day?

由于没有任何建议对我来说是开箱即用的(模式匹配失败,因此没有任何改变),我想知道这里是否涉及一些更新的功能.无论如何,我的 perl 版本是 5.18.2.我正在尝试在 linux cli 中运行命令,如下所示:

since none of the suggestions work out of the box for me (the pattern matching fails and as a result nothing is changed), I wonder if there is some newer functionality involved here. In any case my perl version is 5.18.2. I am trying to run the commands in a linux cli as follows:

perl -ne 's/pattern/replace/gs; print;' <filename>

希望我没有做错什么

推荐答案

最后成功完成我想要的命令如下:

In the end the command that successfully did what I wanted was the following:

perl -0777pne 's/STDOUT_LOG\((.*?)\)(.*?)<<(.*?);/STDOUT_LOG\(\1\,\2\3\);/gs' sensor.cpp

这是Federico Piazza"和zdim"答案的大量叠加.您会注意到我在前面添加了STDOUT_LOG"位,这与我在大量源文件中真正想要的非常相似.我注意到为了正确匹配,必须在正则表达式的末尾包含 /s 运算符,但还要在 perl 可执行文件中添加 -0777 标志.

This is alot a superposition for the answers of "Federico Piazza" and "zdim". You'll notice I prepended the "STDOUT_LOG" bit in the front to really much exactly what I wanted in a large number of source files. I noticed that for correct matching one MUST include the /s operator at the end of the regex , but also add the -0777 flag in the perl executable.

有关运行时 perl 可执行标志的说明,请参阅 perlrun

see perlrun for an explanation of the runtime perl executable flags

这篇关于将多行字符串与 perl regex 匹配并进行替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆