简体   繁体   English

从python脚本执行时无法理解pcregrep的返回码

[英]Unable to understand return code of pcregrep when executed from python script

I am trying to parse a set of regular expression using pcregrep command. 我正在尝试使用pcregrep命令解析一组正则表达式。 This command will be executed from python and the output is formatted and stored in python data structures. 该命令将从python执行,输出格式化并存储在python数据结构中。 I am able to execute all the regular expressions except the below one 我可以执行除以下之一以外的所有正则表达式

"^.{159}-{51}\s*\n.{159}Attempting to start (?:ORM(R)?|CPS)  channel\s*\n.*in\s*\n.*init_n_config\s*\n.*H_ICM_C_MCC_CHANNEL_MANAGER\.C\s*\n.*at  line\s*\n.*\d+\s*$"

I am printing the error code and along with the message I get by executing pcregrep command from python. 我正在打印错误代码,以及通过执行来自python的pcregrep命令得到的消息。

I am getting error code as 256 and empty message I am using commands utility of python. 我正在使用python的commands实用程序获取错误代码为256和空消息。 I am printing cmd_output[0](for return code) and command_output[1] (for error message) I tried executing this from command line and it works perfectly. 我正在打印cmd_output [0](用于返回代码)和command_output [1](用于错误消息),我尝试从命令行执行此命令,它工作正常。 It prints all the matching lines matching the above regular expression. 打印与上述正则表达式匹配的所有匹配行。

Is there anything I am missing here. 有什么我想念的吗? Only the above regular expression is giving error. 仅上述正则表达式给出错误。 Rest of expressions are working absolutely fine. 其余的表达式都可以正常工作。

Return code 256 (when using it via Python subprocess calling methods of various kinds) can mean that the subprocess exited normally with exitcode 1 which for a grep means that it didn't find anything. 返回码256(当通过各种类型的Python子进程调用方法使用时)可能意味着该子进程以退出码1正常退出,对于grep而言,该退出表示未找到任何内容。 So this might not be an error but just a failure to find the desired regexp. 因此,这可能不是错误,而只是找不到所需的正则表达式。

Now, that regexp contains several special characters like asterisks and backslashes. 现在,该正则表达式包含几个特殊字符,例如星号和反斜杠。 There are lots of ways to hand these over in a wrong way so they get interpreted and thus broken in the process. 有很多方法可以将它们以错误的方式移交给他们,以便在处理过程中对它们进行解释和破坏。 I would suggest to have a look at these points. 我建议看一下这些要点。

It would help if you told us more about how you use this regexp pattern, ie please post the code in which it is used and what is happening with the results etc. 如果您告诉我们更多有关如何使用此regexp模式的信息,将对您有所帮助,例如,请发布使用该regexp模式的代码以及结果如何处理等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM