Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Regex ignore some specific words and include some specific words

I have a regex, .*(key|secret).*=.*, any lines which include key or secret will be matched, but i want ignore lines which include words like log, test or special characters like // , /*, *.

Who has any ideas for this problem? Thanks!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could add a negative lookahead assertion to your current regex pattern to blacklist any words/symbols which you don’t want to appear in any matching line:

^(?!.*(?:\b(?:log|test)\b)|[*/]).*\b(?:key|secret)\b.*=.*$

               ^^^ disallow "log", "test", and special symbols
                                       ^^^ match "key" or "secret"

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading