This question is similar to my original post.
Unable to use conditional regex to test my string in python
The reason for posting another new question is that the requirement here is a little different than the original one.
If the given string is a line by line based, the original answer is good enough. But, the answer there cannot cover the case on multiline string. See below
| Test case | Test string | Expect value from bool(re.match(...)) |
|---|---|---|
| 1. Naive match | xxxx |
True |
| 2. Bad model name | xxxx |
False |
| 3. Missing model | xxxx |
True |
I try multiple regex. But, all of them fail on either test case (2) / (3).
| Tried Regex | Failed on Test |
|---|---|
(board add 0/1)? (?(1) (aaa|bbb)) |
2 |
^(?:(?!board add 0/1).)*$|board add 0/1 (?:aaa|bbb) |
2 |
board add 0/1 (aaa|bbb) |
3 |
(?=board add 0/1 )(?:board add 0/1 (aaa|bbb)) |
3 |
Is it possible to write a regex for getting above test case pass?
You can check them on following url
https://regex101.com/r/2l2Qd4/1
NOTE:
- I just want to catch a particular
board add 0/1instead ofboard add 0/\d+- In my actual use case, interfaces may need different models. That's why I am trying to figure out a particular regex for
board add 0/1. Then, I can extend the regex toboard add 0/2toboard add 0/21one by one
- In my actual use case, interfaces may need different models. That's why I am trying to figure out a particular regex for
- Requirements of a valid string
- If
board add 0/1exists in the string, it must be followed by(aaa|bbb). Otherwise, it is invalid - If
board add 0/1does not exists in the string, this is a valid string.
- If