Please pull this function out into separate file and add unit tests for each case you're handling. Document what this function does, and use more descriptive variable names (e.g. "string" is not descriptive). The unit tests should serve to help document and assert correct functionality of each of the handled cases.

More of a nitpick: I think that the flow here with three return cases and one error case is a bit confusing. Can you replace this with more straightforward if and else statements to make the program flow clearer?