Advertisement
here2share

# re_skip_quoted_etc.py

Aug 28th, 2023 (edited)
1,135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.63 KB | None | 0 0
  1. # re_skip_quoted_etc.py
  2.  
  3. import re
  4.  
  5. input_line = '''HEY 'Python World!' THIS IS A TEST LINE "WITH SOME QUOTES".\nHERE IS A LINE WITH ""\"TRIPLE QUOTES"\"" included!\nI'M ALSO GLAD IT'S GREAT AT IGNORING APOSTROPHES.'''
  6. regexed = re.findall(r'("[^"]+"|\'[^\']+\'|"""[^"]+"""|\S+|\’[^\’]+\’|\n\s*|\s+)', input_line)
  7. print(regexed)
  8. print('')
  9.  
  10. output = []
  11. s = ''
  12. for r in regexed:
  13.     if r.startswith(('"', "'")):
  14.         output += [s, r]
  15.         s = ''
  16.     elif '\n' in r:
  17.         output += ['\n']
  18.     else:
  19.         for i, c in enumerate(r):
  20.             s += c.lower() if i % 2 == 0 else c.upper()
  21.         s += ''
  22.  
  23. output += [s]
  24. output = ''.join(output)
  25.  
  26. print(output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement