pythonpython-re

search for all string except new line characters and last string


I have an output that expecting characters before a final string statement such as so:

\ntest
\n
\n{master}

I want to capture anything except the new line characters \n and {master}. For example, this should fail:

\n
\n
\n{master}

I've tried this (?!\n)\w*(?:{master}) and it keeps matching master... any help is appreciated!


Solution

  • I'd be tempted to instead omit those substrings and see what's left

    content.replace("{master}", "").replace("\n", "")
    
    >>> content = """\ntest
    ... \n
    ... \n{master}"""
    >>> content.replace("{master}", "").replace("\n", "")
    'test'
    

    while it seems unlikely in your case, do take some caution in ordering the replacements to at least avoid mashing two lines to form a new substring (such as \n{mas\nter}