简体   繁体   English

Python regex .match无法匹配通过子进程从C ++进程返回的字符串

[英]Python regex .match failing to match in strings returned from a C++ process via subprocess

A string containing the relevant data is sent from a subprocess via the Popen function and stdout. 包含相关数据的字符串是通过Popen函数和stdout从子进程发送的。

 run = subprocess.Popen('evo -x "' + folder+filename+'"', stdout = subprocess.PIPE, stderr = subprocess.PIPE, env={'LANG':'C++'})
 data, error = run.communicate()

The string that prints the relevant information looks like this: 打印相关信息的字符串如下所示:

printf "height= %.15f \ntilt = %.15f (%.15f)\ncen_volume= %.15f\nr_volume= %.15f\n", height, abs(sin(tilt*pi/180)*ring_OR), abs(tilt), c_vol, r_vol;

and yields to the console on print data (with some extra stuff that is also in stdout): 并在print dataprint data到控制台(还有一些额外的东西也在stdout中):

Evolver 2.40, May 8, 2072; Windows OpenGL, 32-bit

Converting to all named quantities...Done.
Enter command: QUIET
height=    0.000211813357854 
tilt=0.0 (0.0)
cen_volume= 0.000000000600000
r_volume= 0.000000003000000
bad

I match it with: 我将其与:

dat = re.match("[\s]*height\s*=\s*([-]?[\.\d]*)[\s]*tilt\s*=\s*([-]?[\.\d]*)[\s]*\(([\s]*[-]?[\.\d]*)\)[\s]*cen_volume\s*=\s*([-]?[\.\d]*)[\s]*r_volume\s*=\s*([-]?[\.\d]*)[\s]*", data)

However, this is returning 'None'. 但是,这将返回“无”。 This exact same .match is used in programs that match the exact same information after it is read in from a txt file instead of stdout, and works perfectly in that case. 此完全相同的.match用于在从txt文件而不是stdout中读取完全相同的信息之后匹配的完全相同的信息,并且在这种情况下可以完美地工作。 Is there something unusual about the way that the string is treated, or some unusual extra non-visible character that is added, when this is retrieved via communicate? 通过通信检索字符串时,字符串的处理方式是否存在异常,或者添加了一些异常额外的非可见字符? Note that .findall works perfectly fine for matching re.findall("[\\s]*(bad)[\\s]*", data) , but even trying to match just a single line with .match still fails. 请注意,.findall可以很好地匹配re.findall("[\\s]*(bad)[\\s]*", data) ,但是即使仅尝试使用.match匹配一行也仍然失败。

two problems: 两个问题:

  1. use re.search as re.match would require the string to match from the first char 使用re.search作为re.match将需要字符串与第一个字符匹配
  2. add re.MULTILINE and re.DOTALL flags for the kind of multiline search you want 添加re.MULTILINEre.DOTALL标志用于所需的多行搜索类型

Try: 尝试:

>>> re.search("[\s]*height\s*=\s*([-]?[\.\d]*)[\s]*tilt\s*=\s*([-]?[\.\d]*)[\s]*\(([\s]*[-]?[\.\d]*)\)[\s]*cen_volume\s*=\s*([-]?[\.\d]*)[\s]*r_volume\s*=\s*([-]?[\.\d]*)[\s]*", v,re.MULTILINE|re.DOTALL).groups()
('0.000211813357854', '0.0', '0.0', '0.000000000600000', '0.000000003000000')

It might be simpler to use findall in this case: 在这种情况下,使用findall可能会更简单:

from pprint import pprint
pprint(dict(re.findall(r"(height|tilt|cen_volume|r_volume)\s*=\s*(.*\S)", data)))

Output 输出量

{'cen_volume': '0.000000000600000',
 'height': '0.000211813357854',
 'r_volume': '0.000000003000000',
 'tilt': '0.0 (0.0)'}

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

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