简体   繁体   中英

How can I find the number of occurences of a substring in a string in python?

I have a substring 'G^ATTC' and I want to find the number of time it occurs in a string like 'ATCGCGATTC' but I cannot because of '^' .

I used re.findall , but the result is always 0 .

This is because in Regex, the "^" character means "the start of the line." Related to this, "$" means "the end of a line"

So, when it's searching for "G^ATTC", it would never match anything, because you're saying the "G" comes before the start of the line (which doesn't even make sense).

The way to fix your regex is to include a "\" to escape the "^". This tells regex to treat the "^" as a character instead of the start of the line.

So, change it to "G\^ATTC"

maybe something like this:

import re

txt = "ATCGCG1ATTCAAAAAAAAAAAAAG4ATTC"
substring =  'G^ATTC'
x = re.findall(substring.replace('^','.'), txt) # ['G1ATTC', 'G4ATTC']
print ("pattern {} occurs {} times".format(substring,len(x)))

output:

pattern G^ATTC occurs 2 times

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM