You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to split a DNA sequence with a restriction enzyme.
A description enzyme can be describe as, e.g. r'(?<CA)(?=GCTG)'
I cannot get re.split to split on this pattern as perl 5 does.
But unfortunately this is backward incompatible change and will likely break existing code (and breaks tests). Consider following example: re.split('(:*)', 'ab'). Currently the result is ['ab'], but with the patch it is ['', '', 'a', '', 'b', '', ''].
In third-part regex module [1] there is the V1 flag which switches incompatible bahavior change.
re.split() with the r'(?<CA)(?=GCTG)' pattern raises a ValueError in 3.5 (see bpo-22818). In future releases it could be changed to work with zero-width patterns (such as lookaround assertions).
Activity
rexdwyer commentedon Nov 7, 2014
I would like to split a DNA sequence with a restriction enzyme.
A description enzyme can be describe as, e.g. r'(?<CA)(?=GCTG)'
I cannot get re.split to split on this pattern as perl 5 does.
ezio-melotti commentedon Nov 7, 2014
Can you provide a sample DNA sequence (or part of it), the exact code you used, the output you got, and what you expected?
serhiy-storchaka commentedon Nov 7, 2014
I think expected output is ['CA', 'GCTG'].
rexdwyer commentedon Nov 7, 2014
sorry if I wasn't clear.
expected output is:
acgtCA|GCTGaaacccCA|GCTGacgtacgt
-> ['ACGTCA', 'GCTGAAACCCCA', 'GCTGACGTACGT']
I would also be able to split a text on word boundaries:
re.split(r'\b', "the quick, brown fox")
-> ['the', ' ', 'quick', ', ', 'brown', ' ', 'fox']
but that doesn't work either so maybe it's a problem with all zero-width matches.
serhiy-storchaka commentedon Nov 7, 2014
This looks as one of existing issue about zero-length matches (bpo-1647489, bpo-10328).
serhiy-storchaka commentedon Nov 8, 2014
It is possible to change this behavior (see example patch). With this patch:
But unfortunately this is backward incompatible change and will likely break existing code (and breaks tests). Consider following example: re.split('(:*)', 'ab'). Currently the result is ['ab'], but with the patch it is ['', '', 'a', '', 'b', '', ''].
In third-part regex module [1] there is the V1 flag which switches incompatible bahavior change.
I don't know how to solve this issue without introducing such flag (or adding special boolean argument to re.split()).
As a workaround I suggest you to use the regex module.
[1] https://pypi.python.org/pypi/regex
serhiy-storchaka commentedon Nov 8, 2014
Previous attempts to solve this issue: bpo-852532, bpo-988761, bpo-3262.
serhiy-storchaka commentedon Mar 2, 2015
re.split() with the r'(?<CA)(?=GCTG)' pattern raises a ValueError in 3.5 (see bpo-22818). In future releases it could be changed to work with zero-width patterns (such as lookaround assertions).