I have two questions:
1) I have a regular expression ([A-Z][a-z]{0,2})(\d*)
and I am using Python's re.finditer()
to match appropriate strings. My problem is, that I want to match only strings that contain no extra characters, otherwise I want to raise an exception.
I want to catch a following pattern: - capital letter, followed by 0, 1 or 2 small letters, followed by 0 or more numbers.
The pattern represents a chemical formula, i.e. atom followed by number of it's occurences. I want to put the atom into a dictionary with it's number of occurences, so I need to separate atoms (capital letter followed by 0, 1 or 2 small letters) and numbers, but remember that they belong together.
Example:
C6H5Fe2I # this string should be matched successfully. Result: C6 H5 Fe2 I
H2TeO4 # this string should be matched successfully Result: H2 Te O4
H3PoooO5 # exception should be raised
C2tH6 # exception should be raised
2) second question is what kind of Exception should I raise in case the input string is wrong.
Thank you, Tomas