textpad 8 regular expression with multiple conditions

1

I've searched through all of the TextPad regex postings on superuser.com and have not found an answer to my query. It is -- how can I provide two conditions in a TextPad 8 file search regular expression? In particular, I want to find all lines in all files that contain the strings Error or Warning, which I can do using the regex error|warning, but in addition to that, I want to select only the subset of those lines where another specified text string, e.g. expir, is not present anywhere on the lines, before or after the location of the matching string from the first regex.

I've tried various forms of putting a conjunction like & or && between the two regexes, but cannot find a syntax that works. Do TextPad regular expressions include support for lookahead and lookbehind zero-width assertions? In perl, I could say

    (?<!expir).*?error|warning(?!.*?expir)

. I entered that in TextPad, and it caused no errors, but it also did not work. It selected all of the lines that contained either error or warning but did not exclude lines that also contained expir.

fireblood

Posted 2017-09-19T15:59:59.920

Reputation: 139

To test if something is NOT present, you have to use this construction: ((?!expir).)* so your regex becomes: ^((?!expir).)*(?:error|warning)((?!expir).)*$ – Toto – 2017-09-20T09:47:50.427

Answers

2

This regex will find what you want:

^(?=(?:(?!expir).)*$).*(?:error|warning)

Explanation:

^                       : begining of line
    (?=                 : start lookahead
        (?:             : start non capture group
            (?!expir)   : negative lookahead, make sure w don'thave expir
                            You may want to add wordboundaries if you don't want to match "expiration"
                            (?!\bexpir\b)
            .           : any character but newline
        )*              : group may appear 0 or moe times
        $               : end of line
    )                   : end of lookahead
                            at this point we are sure we don't have "expir" in the line
                            so, go to match the wanted words
    .*                  : 0 or more any character but newline
    (?:                 : start non capture group
        error           : literally error, you could do "\berror\b" if you don't want to match "errors"
        |               : OR
        warning         : literally warning, you could do "\bwarning\b" if you don't want to match "warnings"
    )   

With file like:

error
warning
abc expir 
abc expir warning
abc error expir def

it matches lines 1 and 2 only.

Toto

Posted 2017-09-19T15:59:59.920

Reputation: 7 722