简体   繁体   English

你如何找到一个子字符串是否不在字符串中作为正则表达式?

[英]how do you find if a sub-string is not in a string as a regex?

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.我想要的是让用户传递一个正则表达式,如果没有匹配,它将返回 True。 For instance: exit if you don't see 00002 , or exit if you don't see the word test .例如:如果没有看到00002退出,如果没有看到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.在大多数情况下,除了否定匹配之外,re 的语法是不言自明的。 Thank you!谢谢!

This should work:这应该有效:

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

This answer explains it better than I could: https://stackoverflow.com/a/406408这个答案比我能更好地解释它: 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.我还建议您创建正则表达式来查找所需的字符串,然后在您的代码中反转它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM