import re
text = "Your input string line 1\nLine 2 with fiction\nLine 3 with non-fiction\nLine 4 with fiction and non-fiction"
rx = re.compile(r'\b(?<!non-)fiction\b')
# Approach with regex returning any line containing fiction with no non- prefix:
final_output = [line.strip() for line in text.splitlines() if rx.search(line)]
print(final_output)
# Non-regex approach that does not return lines that may contain non-fiction (if they contain fiction with no non- prefix):
final_output = [line.strip() for line in text.splitlines() if "fiction" in line and "non-fiction" not in line]
print(final_output)
aW1wb3J0IHJlCnRleHQgPSAiWW91ciBpbnB1dCBzdHJpbmcgbGluZSAxXG5MaW5lIDIgd2l0aCBmaWN0aW9uXG5MaW5lIDMgd2l0aCBub24tZmljdGlvblxuTGluZSA0IHdpdGggZmljdGlvbiBhbmQgbm9uLWZpY3Rpb24iCnJ4ID0gcmUuY29tcGlsZShyJ1xiKD88IW5vbi0pZmljdGlvblxiJykKIyBBcHByb2FjaCB3aXRoIHJlZ2V4IHJldHVybmluZyBhbnkgbGluZSBjb250YWluaW5nIGZpY3Rpb24gd2l0aCBubyBub24tIHByZWZpeDoKZmluYWxfb3V0cHV0ID0gW2xpbmUuc3RyaXAoKSBmb3IgbGluZSBpbiB0ZXh0LnNwbGl0bGluZXMoKSBpZiByeC5zZWFyY2gobGluZSldCnByaW50KGZpbmFsX291dHB1dCkKIyBOb24tcmVnZXggYXBwcm9hY2ggdGhhdCBkb2VzIG5vdCByZXR1cm4gbGluZXMgdGhhdCBtYXkgY29udGFpbiBub24tZmljdGlvbiAoaWYgdGhleSBjb250YWluIGZpY3Rpb24gd2l0aCBubyBub24tIHByZWZpeCk6CmZpbmFsX291dHB1dCA9IFtsaW5lLnN0cmlwKCkgZm9yIGxpbmUgaW4gdGV4dC5zcGxpdGxpbmVzKCkgaWYgImZpY3Rpb24iIGluIGxpbmUgYW5kICJub24tZmljdGlvbiIgbm90IGluIGxpbmVdCnByaW50KGZpbmFsX291dHB1dCk=