-2

I want to find if a particular sub-string is not present in a file to exit the output.

For example, let the file have the contents below:

Time|Code 1| Code 2 | Message string
2019-04-03 12:05:29,006|00002| D8456| This is a test 
2019-04-03 12:05:29,006|00003| D8457| This is a test

what I want is have the user pass a regex that will return True if there is no match. For instance: exit if you don't see 00002, or exit if you don't see the word test.

update: this will exit on the condition passed. I'm just trying to find the correct syntax for the below case. it could exit if the condition is found, condition is not found, any of the conditions are found. for the most part the syntax of re is pretty self explanatory except the negative match. Thank you!

  • 1
    Have you looked at [`includes()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) – Mark Apr 03 '19 at 16:23
  • 2
    "not (substring in filecontents)" how is the regex involved? If you need to match a regex do that and use not. I don't see any code here. What have you tried? – Kenny Ostrom Apr 03 '19 at 16:26
  • 1
    `return not bool(re.match(...))` – mypetlion Apr 03 '19 at 16:29
  • sorry, this will be used for both positive and negative match. exit if string is found, exit is string is not found – Oscar Flores Apr 03 '19 at 17:09

1 Answers1

0

This should work:

^((?!your text here).)*$

This answer explains it better than I could: https://stackoverflow.com/a/406408

I would also recommend maybe creating regex that looks for the desired string, and then inverting that in your code.

Shono 1
  • 46
  • 6
  • this actually doesn't seem to work when there is a \n in the string: This works correctly: `input_data="testtest2"` `r='^((?!X).)*$' `re.findall(r,input_data)` This does not: `input_data="test\ntest2"` `r='^((?!X).)*$'` `re.findall(r,input_data)` – Oscar Flores Apr 03 '19 at 17:33