1

I have a large number of txt-files and aim to extract a certain piece of text out of each of them. I managed to this for most of my files using regex in notepad. I just use find-and-replace for all files in a folder using a pretty elaborate regex:

Find:

.*(pretty elaborate regex).*

Replace:

$1

This works great. However, some of my documents do not contain the specific piece of text I am looking for. Notepad will leave these documents unchanged. How can I let it put a text in these documents which says for example: "no_match_found"

Thanks for you help! Andreas

Andreas K
  • 63
  • 6

2 Answers2

1

Maybe this post will help you: How to negate the whole regex?

Use negative lookaround: (?!pattern)

You can just match for the negated regex and place the text then

Community
  • 1
  • 1
meetaig
  • 913
  • 10
  • 26
1

It is quite easy with the Boost conditional replacement pattern. Add a |(.*) alternative branch to your regex (make sure the . matches newline is ON) and use (?1$1:NO_MATCH_FOUND) pattern that will eaither insert the Group 1 value, or will just replace the whole document with the literal text NO_MATCH_FOUND.

Here is an example of what happens to a document having no match:

enter image description here

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563