Maxwell
Maxwell

Reputation: 429

string matching with substitution using PYTHON

I have a string and I need match that string with an sequence and determine the number of times the matched sequence is found in that sequence But it has following conditions Sequence can contain only ACGT valid chars so seq could be ACGTGTCTG

the string could be ACGnkG where n could be replaced by A or G k could be replaced by C or T

how can we find if the string matches the sequence by substituting valid values for n and k

Is there any regular expression ?

Upvotes: 0

Views: 145

Answers (2)

jfs
jfs

Reputation: 414139

If you want to count occurrences of the pattern:

count_regex = sum(1 for _ in re.finditer(r'ACG[AG][CT]G', s))

If you want to count occurrences of a fixed string that matches first the pattern:

m = re.search(r'ACG[AG][CT]G', s)
count_fixed = s.count(m.group(0), m.start(0)) if m else 0

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200213

re.findall(pattern, string) will return a list with all matches for pattern in string. len(...) will return the number of items in a list.

Upvotes: 2

Related Questions