fork download
  1. import re
  2.  
  3. strings = [
  4. r"C:\temp\John Smith Junior\file.pdf",
  5. r"C:\temp\John Smith Junior\John Smith Junior\file.pdf",
  6. r"C:\temp\John Smith Junior file.pdf"
  7. ]
  8.  
  9. for s in strings:
  10. m = re.match(r".*?\bJunior\b(\\)?(?(1)|.*)", s)
  11. if m:
  12. print(m.group())
Success #stdin #stdout 0.02s 9376KB
stdin
Standard input is empty
stdout
C:\temp\John Smith Junior\
C:\temp\John Smith Junior\
C:\temp\John Smith Junior file.pdf