Then I have this sequence and I want to use regex's search function to find the field before the query event
This is what I have.
sequence = 'abcdefghijklmnopqrstuvwxyz' query = 'jklmnop' That's what I want to end with the = 'qrstuvwxyz' I tried it earlier and this is not working Has been doing. I thought it would divide it into 3 groups
sequence = 'abcdefghijklmnopqrstuvwxyz' query = 'jklmnop' print re.search ('\ w + (query) \ w +', sequence). Group (0) traceback (most recent call final): File "& lt; stdin>", line 1, & lt; Module & gt; AttributeError: The 'any type' object has no attribute 'group'
< Writing the code> query does not store the value of query inside a string in that string, it only creates a new string in the string 'query'. & gt; & Gt; & Gt; Print '\ w + (query) \ w +' w + (query) \ w + You should use string formatting:
Gt; & Gt; & Gt; Sequence = 'abcdefghijklmnopqrstuvwxyz' & gt; & Gt; & Gt; Query = 'jklmnop' & gt; & Gt; & Gt; '(\ W +) ({}) (\ w +)'. Format (query) '(\\ w +) (jklmnop) (\\ w +)' & gt; & Gt; & Gt; Re.search ('(w)) ({}) (\ w +)'. Format (query), sequence). Group (1) 'abcdefghi' & gt; & Gt; & Gt; (Search) (.) (Group) (3) 'qrstuvwxyz' is better to use re.split for such purposes: & gt; & Gt; & Gt; Strs = 'abcdefghijklmnopqrstuvwxyz' & gt; & Gt; & Gt; Import re & gt; & Gt; & Gt; First, after re.split ('jklmnop', strs), & gt; & Gt; & Gt; Before 'Abcdefghi' & gt; & Gt; & Gt; After 'Qrstuvwxyz'
No comments:
Post a Comment