首页 > 解决方案 > 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 :)

标签: pythonregexpython-3.x

解决方案


尝试这个:

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)

推荐阅读