可选列的正则表达式

时间:2018-09-29 21:56:02

标签: regex text rust

Rust的{​​{1}}库中,我使用以下正则表达式从文本文件中读取行并将其存储到某些数据存储中。问题在于,在某些情况下,该列之一可能是缺失值,因为它是可选的。但是,当发生这种情况时,正则表达式机制将忽略整行。如何通知特定列是可选的,以便结果可以识别行的其他部分并用空字符串填充空列?

示例:

regex 1.0.2

表达式:5 "Stanford Encyclopedia of Philosophy" "Product" 6 "Spatial History Project" 7 "Humanities Research Network" "Group"`

当前结果:

(?P<n_id>\\d+)\\s+"(?P<n_label>[^"]*)"\\s+"(?P<n_type>[^"]*)"

理想的结果:

Captures({0: Some("5\t\"Stanford Encyclopedia of Philosophy\"\t\"Product\""), "n_id": Some("5"), "n_label": Some("Stanford Encyclopedia of Philosophy"), "n_type": Some("Product")})
Captures({0: Some("7\t\"Humanities Research Network\"\t\"Group\""), "n_id": Some("7"), "n_label": Some("Humanities Research Network"), "n_type": Some("Group")})

0 个答案:

没有答案
相关问题