Advertisement
here2share

# regex_lookarounds.py

Jun 25th, 2016
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. # regex_lookarounds.py
  2. import re
  3.  
  4. # Found many examples that gave faulty results... so I hereby provide my own with... (?!) ... to prove as an accurate explanation
  5.  
  6. z = 'abcxyzXYZABC'
  7. def x(a):
  8.     print re.findall(a,z)
  9. x( '(?i)abc(?=xyz)'     )   # finds the 1st abc ("abc" which has "ABC" after it)
  10. x( '(?i)abc(?!xyz)'     )   # finds the 2nd abc ("ABC" which does not have "abc" after it)
  11. x( '(?i)(?<=xyz)abc'    )   # finds the 2nd abc ("ABC" which does not have "abc" after it)
  12. x( '(?i)(?<!xyz)abc'    )   # finds the 1st abc ("abc" which has "ABC" after it)
  13.  
  14. z = 'abc. xyz X. XYZ .A. A.B.C'
  15. print re.sub('(?<= [A-z])\. ',' ',z)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement