Home > Blockchain >  I need a python re to match only if two strings are absent and two other strings are present
I need a python re to match only if two strings are absent and two other strings are present

Time:12-01

Using python 3.9.5, I have this string

>>> t
' LICENSE INVALID\n Your license does not include module AMS version 2020.103 on this machine.\n Module AMS\n LICENSE INVALID\n Module AMS version2020.103\n Your license does not include module AMS version 2020.103 on this machine.\n Module AMS\n Module AMS version2020.103\nLICENSE INVALID\nLICENSE INVALID'

I want a re that will return None if either one of the strings 'LICENSE INVALID' or 'license does not include' is found; and, if those strings are both absent and both of the strings '2020.103' and 'NORMAL TERMINATION' are present, only then do I want it to return a match. (If nothing at all matches, return a None too.) So far I have

>>> p=re.compile(r'^(?!.*LICENSE INVALID|license does not include).*(?:2020.103|NORMAL TERMINATION).*')
>>> print(p.search(s))
<re.Match object; span=(0, 8), match='2020.103'>

This does the first part: it returns None if 'LICENSE INVALID' or 'license does not include' are in the text. However, I believe it is doing an exclusive "or" match on the latter two strings. I want it to do an "and". It matches above when I'd rather it did not. The output I'm matching on will likely contain '2020.103' both when there is a failure (when I do not want my re to find a match) or a success (when I want my re to find a match). I need to use a re for this to fit it in with someone else's code I'm using. To summarize: only if '2020.103' and 'NORMAL TERMINATION' are both found, and 'LICENSE INVALID' and 'license does not include' are not found, do not return a None.

CodePudding user response:

With regex, would you please try:

p = re.compile(r'^(?!.*(?:LICENSE INVALID|license does not include)).*(?=.*2020.103)(?=.*NORMAL TERMINATION)')

It should match if '2020.103' and 'NORMAL TERMINATION' are both found, and any of 'LICENSE INVALID' and 'license does not include' are not found.

CodePudding user response:

I might avoid regex here and instead use the base string functions:

inp = [' LICENSE INVALID\n Your license does not include module AMS version 2020.103 on this machine.\n Module AMS\n LICENSE INVALID\n Module AMS version2020.103\n Your license does not include module AMS version 2020.103 on this machine.\n Module AMS\n Module AMS version2020.103\nLICENSE INVALID\nLICENSE INVALID', 'Hello 2020.103 is NORMAL TERMINATION']

for x in inp:
    if "LICENSE INVALID" not in x and "license does not include" not in x and "2020.103" in x and "NORMAL TERMINATION" in x:
        print("MATCH: "   x)
    else:
        print("NO MATCH: "   x)

Only the second sample input in the list is matching.

  • Related