python - Python Regular Exression assistance
问题描述
Sorry for the generic title.
I have this text:
----------------------------------------------- One Errors ------------------------------------------------------
VALUES1 64 0 0.00
VALUES2 0 0 0.00
VALUES3 2535 0 0.00
VALUES4 0 0 0.00
ALL 2674 2674 100.00 5.31 3.60 4.70 5.70 8.30 10.80 20.90 27.50 31.10 36.53 [Free Text]
-----------------------------------------------Two Errors ------------------------------------------------------
VALUES1 64 0 0.00
VALUES2 0 0 0.00
VALUES3 2535 0 0.00
VALUES4 0 0 0.00
ALL 2674 0 0.00
-----------------------------------------------Three Errors ------------------------------------------------------
VALUES1 64 0 0.00
VALUES2 0 0 0.00
VALUES3 2535 0 0.00
VALUES4 0 0 0.00
ALL 2674 2674 100.00 1.51 0.70 1.10 1.60 3.30 4.50 5.40 6.40 9.50 12.17 [Free Text]
-----------------------------------------------Four Errors ------------------------------------------------------
VALUES1 64 0 0.00
VALUES2 0 0 0.00
VALUES3 2535 0 0.00
VALUES4 0 0 0.00
ALL 2674 2674 100.00 0.34 0.10 0.17 0.27 0.67 1.10 1.48 1.97 2.32 3.12 [Free Text]
-----------------------------------------------Five Errors ------------------------------------------------------
VALUES1 64 0 0.00
VALUES2 0 0 0.00
VALUES3 2535 0 0.00
VALUES4 0 0 0.00
ALL 2674 0 0.00
VALUES1 64 0 0.00
VALUES2 0 0 0.00
VALUES3 2535 0 0.00
VALUES4 0 0 0.00
ALL 2674 2674 100.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 [Free Text]
VALUES1 64 0 0.00
VALUES2 0 0 0.00
VALUES3 2535 0 0.00
VALUES4 0 0 0.00
ALL 2674 0 0.00
As you can see in some cases I have a row(starts with ALL) contains 14 columns and sometimes only 3(not including the "ALL" value).
I need to grab columns 3, 6, 8, 10, 13 from each row starts with "ALL" and contains values within the section.
for example:
in "One Errors" section: 100.00, 4.70, 8.30, 20.90, 36.53
in "Two Errors" section: 0.00, None, None, None, None
I was trying to use in "Five Errors" section: 100.00, 0.00, 0.00, 0.00, 0.00
I was trying to use this regex:
Tow Errors[\s\S]*?ALL\s+\S+\s+\S+\s+(\S+)\s+\S+\s+\S+\s+(\S+)\s+\S+\s+(\S+)\s+\S+\s+(\S+)\s+\S+\s+\S+\s+(\S+).*?$
and also
Tow Errors[\s\S]*?ALL\s+[0-9\.]+?+\s+[0-9\.]+?\s+([0-9\.]+?)\s+[0-9\.]+?\s+[0-9\.]+?\s+([0-9\.]+?)\s+[0-9\.]+?\s+([0-9\.]+?)\s+[0-9\.]+?\s+([0-9\.]+?)\s+[0-9\.]+?\s+[0-9\.]+?\s+([0-9\.]+?).*?$
Obviously im doing something wrong here and need your advice Thanks :)
解决方案
尝试这个:
regex = 'ALL(?:[ \t]+\S+){2}([ \t]+\S+)?(?:(?:[ \t]+\S+){2}([ \t]+\S+)?)?(?:(?:[
\t]+\S+)([ \t]+\S+)?)?(?:(?:[ \t]+\S+)([ \t]+\S+)?)?(?:(?:[ \t]+\S+){2}([ \t]+\S+)?)?'
re.findall(regex, string)
推荐阅读
- python - CNN 没有提高其性能
- sql - 等效于 hive 中 Presto 的 transform() 函数
- javascript - Service Worker 不会缓存 golang 服务器在“/”上提供的 index.html
- python - Python删除多个文件夹中的文件
- c++ - 我可以在课堂上使用 omnetpp.ini 变量吗?
- javascript - ElectronJS 为当前用户自动启动,但不适用于机器上的所有用户
- node.js - 使用pdfkit时如何解决节点js中的错误“Cannot read property 'write' of null”
- mysql - 传输数据库时出错 - #1064 - 您的 SQL 语法有错误
- python - 调用方法返回 <__main__ XYZ object at 0x01234567>
- xslt - 如何使用 xslt 在评论节点中将 & 更改为 &