text: user2523: Aim is to code the theoretical peptic cleavage of protein sequences in Python. The cleavage (cutting) rule for pepsin is: 1234^567. This is a position, ^ stands for the cleavage point. 
  There are rules that it mustn't have at all and rules where it must have at least one. The rule for mustn't is in position 2, it mustn't be R, K or H. In position 3 and 7, there can't be a P and in position 4, there mustn't be an R. 
  If it follows "the must not" rules, then it will check "the must" rules so that if any of one them is met, it will cleave. So if there is a P in position 1 or 2, or if there's an L, W, Y or F in position 4 or 5 then, it will be cleaved (use newline).
  This is the code I have so far, but it doesn't work yet:
  import re
  print('INSERT YOUR PEPTIDE') 
  X= input(); 
  r=re.findall('(?<=[P])(?<=[P,^R,^K,^H])(?<=([^P])(?<=[L,W,F,Y,^R])(?<=[L,W,Y,F])/D(?<=[^P])',X) 
  print(r)
  
  My input is:
  TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI 
  
  How can I make this work?
  
  user298: I am really not sure if I've understood your rules correctly, but I think you want this:
  
  If all of these are true:
  
  position 2: not R, K or H
  position 3: not P
  position 4: not R
  position 7: not P
  
  And, if at least one of these is true:
  
  position 1: P
  position 1: P
  position 4: L or W or Y or F
  position 5: L or W or Y or F
  
  
  Then cleave.
  If so, then you probably don't want to do it using regular expressions. First because it will be hard, and second because it will be absolutely awful to maintain if you need to come back to it. Instead, try something like this:
  #!/usr/bin/env python3
  # X = input(); 
  X = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI"
  seq = list(X)
  for i, res in enumerate(seq):
      if i > len(seq) - 7:
          break
      if (
              seq[i+1] in ('R','K','H') or
              seq[i+2] == 'P' or
              seq[i+3] == 'R' or
              seq[i+6] == 'P'
      ):
          continue
      if res == 'P':
          start = i
      elif seq[i+1] == 'P':
          start = i+1
      else:
          continue
      if (
              seq[i+4] in ('L', 'W', 'Y', 'F') or
              seq[i+5] in ('L', 'W', 'Y', 'F')
      ):
          print("%s\n%s\n%s" %
              ("".join(seq[0:start]),
               "".join(seq[start:start+7]),
               "".join(seq[start+7:])
              )
          )
  
  This will produce the following output when run:
  $ foo.py
  TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMV
  PKCFNEL
  LMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI
  
  
  If you insist on using regular expressions, you could separate it into two expressions and use something like this:
  import re
  X = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI"
  re1='P[^RKH][^P][^R][LWYF].[^P]'
  re2='P[^P][^R][LWYF].[^P]'
  r=re.findall('(?:P[^RKH][^P][^R][LWYF].[^P])|(?:P[^P][^R][LWYF].[^P])',X) 
  print(r)
  
  Before you do, however, please consider the wise words of XKCD 208:
  
  
  user2523: Ok I think I almost solve this problem so I still using RE 
  import re
  myprotein = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI"
  my_protein = []
  for protein in myprotein:
      myprotein = protein.rstrip('\n')
      my_protein.append(myprotein)
      my_pro = (''.join(my_protein))
  
     #cleaves sequence    
  peptides = re.sub(r'(?<=[^HRK][^P][^R])(?=[FLWY][^P])|(?<=[^HRK][^P][FLWY])(?=.[^P])','\n', my_pro)
  
  print (peptides)
  
  and it produce to 
  TTG
  W
  M
  F
  KG
  LY
  Q
  L
  KSDEARMHN
  LQRW
  YV
  F
  KVIE
  F
  W
  GINRKDQHDSRY
  WF
  SH
  F
  CW
  K
  L
  EHGMVPKC
  FNE
  L
  LMPPMSTHRSMF
  NA
  Y
  SMS
  F
  CVQVKE
  WRGSISRMCDHHQQQCQ
  Y
  W
  RL
  MAGGT
  LI
  
  but the true answer should be like (checking from https://web.expasy.org/peptide_cutter/ )
  TTG
  W
  M
  F
  KG
  LY
  Q
  L
  KSDEARMHN
  LQRW
  YV
  F
  KVIE
  F
  W
  GINRKDQHDSRY
  WF
  SH
  F
  CW
  K
  L
  EHGMVPKC
  FNE
  L
  LMPPMSTHRSMF
  NA
  Y
  SMS
  F
  CVQVKE
  WRGSISRMCDHHQQQCQ
  Y
  W
  RL
  MAGGT
  L
  I 
  
  so how can I make this work?
  
original_text: 2523: <p>Aim is to code the theoretical peptic cleavage of protein sequences in Python. The cleavage (cutting) rule for pepsin is: <code>1234^567</code>. This is a position, <code>^</code> stands for the cleavage point. </p>
  
  <p>There are rules that it mustn't have at all and rules where it must have at least one. The rule for mustn't is in position 2, it mustn't be R, K or H. In position 3 and 7, there can't be a P and in position 4, there mustn't be an R. </p>
  
  <p>If it follows "the must not" rules, then it will check "the must" rules so that if any of one them is met, it will cleave. So if there is a P in position 1 or 2, or if there's an L, W, Y or F in position 4 or 5 then, it will be cleaved (use newline).</p>
  
  <p>This is the code I have so far, but it doesn't work yet:</p>
  
  <pre><code>import re
  print('INSERT YOUR PEPTIDE') 
  X= input(); 
  r=re.findall('(?&lt;=[P])(?&lt;=[P,^R,^K,^H])(?&lt;=([^P])(?&lt;=[L,W,F,Y,^R])(?&lt;=[L,W,Y,F])/D(?&lt;=[^P])',X) 
  print(r)
  </code></pre>
  
  <p>My input is:</p>
  
  <pre><code>TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI 
  </code></pre>
  
  <p>How can I make this work?</p>
   298: <p>I am really not sure if I've understood your rules correctly, but I think you want this:</p>
  
  <ul>
  <li><p>If all of these are true:</p>
  
  <ul>
  <li>position 2: not R, K or H</li>
  <li>position 3: not P</li>
  <li>position 4: not R</li>
  <li>position 7: not P</li>
  </ul></li>
  <li><p>And, if at least one of these is true:</p>
  
  <ul>
  <li>position 1: P</li>
  <li>position 1: P</li>
  <li>position 4: L or W or Y or F</li>
  <li>position 5: L or W or Y or F</li>
  </ul></li>
  </ul>
  
  <p>Then cleave.</p>
  
  <p>If so, then you probably don't want to do it using regular expressions. First because it will be hard, and second because it will be absolutely awful to maintain if you need to come back to it. Instead, try something like this:</p>
  
  
  
  <pre><code>#!/usr/bin/env python3
  # X = input(); 
  X = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI"
  seq = list(X)
  for i, res in enumerate(seq):
      if i &gt; len(seq) - 7:
          break
      if (
              seq[i+1] in ('R','K','H') or
              seq[i+2] == 'P' or
              seq[i+3] == 'R' or
              seq[i+6] == 'P'
      ):
          continue
      if res == 'P':
          start = i
      elif seq[i+1] == 'P':
          start = i+1
      else:
          continue
      if (
              seq[i+4] in ('L', 'W', 'Y', 'F') or
              seq[i+5] in ('L', 'W', 'Y', 'F')
      ):
          print("%s\n%s\n%s" %
              ("".join(seq[0:start]),
               "".join(seq[start:start+7]),
               "".join(seq[start+7:])
              )
          )
  </code></pre>
  
  <p>This will produce the following output when run:</p>
  
  <pre><code>$ foo.py
  TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMV
  PKCFNEL
  LMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI
  </code></pre>
  
  <hr>
  
  <p>If you insist on using regular expressions, you could separate it into two expressions and use something like this:</p>
  
  <pre><code>import re
  X = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI"
  re1='P[^RKH][^P][^R][LWYF].[^P]'
  re2='P[^P][^R][LWYF].[^P]'
  r=re.findall('(?:P[^RKH][^P][^R][LWYF].[^P])|(?:P[^P][^R][LWYF].[^P])',X) 
  print(r)
  </code></pre>
  
  <p>Before you do, however, please consider the wise words of <a href="https://www.xkcd.com/1171/" rel="nofollow noreferrer">XKCD 208</a>:</p>
  
  <p><a href="https://i.stack.imgur.com/lIrt1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lIrt1.png" alt="XKCD 1171"></a></p>
   2523: <p>Ok I think I almost solve this problem so I still using RE </p>
  
  <pre><code>import re
  myprotein = "TTGWMFKGLYQLKSDEARMHNLQRWYVFKVIEFWGINRKDQHDSRYWFSHFCWKLEHGMVPKCFNELLMPPMSTHRSMFNAYSMSFCVQVKEWRGSISRMCDHHQQQCQYWRLMAGGTLI"
  my_protein = []
  for protein in myprotein:
      myprotein = protein.rstrip('\n')
      my_protein.append(myprotein)
      my_pro = (''.join(my_protein))
  
     #cleaves sequence    
  peptides = re.sub(r'(?&lt;=[^HRK][^P][^R])(?=[FLWY][^P])|(?&lt;=[^HRK][^P][FLWY])(?=.[^P])','\n', my_pro)
  
  print (peptides)
  </code></pre>
  
  <p>and it produce to </p>
  
  <pre><code>TTG
  W
  M
  F
  KG
  LY
  Q
  L
  KSDEARMHN
  LQRW
  YV
  F
  KVIE
  F
  W
  GINRKDQHDSRY
  WF
  SH
  F
  CW
  K
  L
  EHGMVPKC
  FNE
  L
  LMPPMSTHRSMF
  NA
  Y
  SMS
  F
  CVQVKE
  WRGSISRMCDHHQQQCQ
  Y
  W
  RL
  MAGGT
  LI
  </code></pre>
  
  <p>but the true answer should be like (checking from <a href="https://web.expasy.org/peptide_cutter/" rel="nofollow noreferrer">https://web.expasy.org/peptide_cutter/</a> )</p>
  
  <pre><code>TTG
  W
  M
  F
  KG
  LY
  Q
  L
  KSDEARMHN
  LQRW
  YV
  F
  KVIE
  F
  W
  GINRKDQHDSRY
  WF
  SH
  F
  CW
  K
  L
  EHGMVPKC
  FNE
  L
  LMPPMSTHRSMF
  NA
  Y
  SMS
  F
  CVQVKE
  WRGSISRMCDHHQQQCQ
  Y
  W
  RL
  MAGGT
  L
  I 
  </code></pre>
  
  <p>so how can I make this work?</p>
  