How to count the number of strings matched by regular expressions in python2.7?

clipboard.png

as shown in the figure above, there is a multi-line string returned by the linux command. You need to cut the first "CLA", of each line under the INCLUDE column and count the total number of" CLA" "returned under the INCLUDE column of all rows (only the first one for each line). My code is as follows:
import re
ss = "PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP+ n | ID | NAME | DESCRIPTION | INCLUDE | ROLLBACK | nPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP+ n | 1481439 | CSS Upgrade Patch | Upgrade CSS to R9A for case | CLA-sharp1481439 | | True | n | 1501340 | Artifact CLA-sharp1501340 ZBX Ansi | support ZBX Ansible Module Ex | CLA-sharp1501340v6.1.5 CLA-sharp1499108v6.1.0 | True | n | 1527137 | Artifact CLA-sharp1527137v6.1.2 PR | support CMCC PRMTOOL Feature | CLA-sharp1527137v6.1.2 | | | True |.

|

for line in ss.split ("n") [3:]:

info = line[1:-1].split("|")
a=re.search(r"(.*?)CLA", info[3])
b = a.group()
print b
c = b.count("CLA")
print c

the results are as follows:
CLA
1
my question is: the three CLA strings in the table have been regularly matched, but only three 1s have been counted. How to achieve the return value with a total of 3 through code? Thank you.


seeing that the linux command returns , my first feeling is that it can be solved directly through the command. You can try:

xxxx  |  awk -F '|' '$5~/^ *CLA-sharp/{nPP}END{print n}'

if you have to use python , copy part of the document directly:

import re
ss = 'PPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP+ n| ID | NAME | DESCRIPTION | INCLUDE | ROLLBACK | nPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPPP+ n| 1481439 | CSS Upgrade Patch | Upgrade CSS to R9A for case | CLA-sharp1481439 | True | n| 1501340 | Artifact CLA-sharp1501340 ZBX Ansi| support ZBX Ansible Module Ex| CLA-sharp1501340v6.1.5 CLA-sharp1499108v6.1.0 | True | n| 1527137 | Artifact CLA-sharp1527137v6.1.2 PR| support CMCC PRMTOOL Feature | CLA-sharp1527137v6.1.2 | True |.........'
print len(re.findall('\| CLA-sharp', ss, re.M))
Menu