sgt ashy
sgt ashy

Reputation: 11

Searching for number of repeated patterns in text file using Python

I wanted to know any suggestions on a code which I can use to output the number of found patterns in a text file.

Example: I have a text file with ("ABBAABBBAAABAB") and I want to search for a certain pattern like ("ABA") but I dont want it to count the last letter as part of the pattern, only new letters.

I've tried using split() and count() but it seems to give me numbers found on each line instead of the entire file and when I dont use split() to put it into a list it just prints 0.

Example its giving me this:

0
0
0
0
0 

And when split it kind of works but doesnt give me the sum only for each line for example:

23
32
12
20
15

How can i get the sum of the pattern im searching for in the entire file just one number like 150 etc...

Would really appreciate some help thank you

Upvotes: 0

Views: 138

Answers (2)

 import re
 data="ABBAABBBAAABABABA"
 matches=re.finditer(r"(ABA)+",data)
 for match in matches:
      print(match)

output:

 <re.Match object; span=(10, 13), match='ABA'>
 <re.Match object; span=(14, 17), match='ABA'>

Upvotes: 0

Sandeep Singh
Sandeep Singh

Reputation: 508

import re
input_text = 'ABBAABBBAAABAB'
pattern = 'ABA'
# removing last letter
input_text = input_text[:-1]
result = len([*re.finditer(pattern, input_text)])

Result will have the desired count.

Upvotes: 3

Related Questions