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:
我有大量的txt文件,旨在从每个文件中提取一段文本。我在记事本中使用正则表达式为我的大部分文件设法了。我只是使用相当复杂的正则表达式对文件夹中的所有文件使用find-and-replace:
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"
这很好用。但是,我的一些文档中不包含我要查找的特定文本。记事本将保持这些文档不变。我怎么能让它在这些文件中加上一个文字,例如:“no_match_found”
Thanks for you help! Andreas
谢谢你的帮助!安德烈亚斯
2 个解决方案
#1
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
.
使用Boost条件替换模式非常容易。在你的正则表达式中添加一个|(。*)替代分支(确保。匹配换行符为ON)并使用(?1 $ 1:NO_MATCH_FOUND)模式,该模式将插入Group 1值,或者只用整个文档替换整个文档文字文本NO_MATCH_FOUND。
Here is an example of what happens to a document having no match:
以下是没有匹配的文档会发生什么情况的示例:
#2
1
Maybe this post will help you: How to negate the whole regex?
也许这篇文章会帮助你:如何否定整个正则表达式?
Use negative lookaround: (?!pattern)
使用否定的外观:(?!pattern)
You can just match for the negated regex and place the text then
您可以匹配否定的正则表达式然后放置文本
#1
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
.
使用Boost条件替换模式非常容易。在你的正则表达式中添加一个|(。*)替代分支(确保。匹配换行符为ON)并使用(?1 $ 1:NO_MATCH_FOUND)模式,该模式将插入Group 1值,或者只用整个文档替换整个文档文字文本NO_MATCH_FOUND。
Here is an example of what happens to a document having no match:
以下是没有匹配的文档会发生什么情况的示例:
#2
1
Maybe this post will help you: How to negate the whole regex?
也许这篇文章会帮助你:如何否定整个正则表达式?
Use negative lookaround: (?!pattern)
使用否定的外观:(?!pattern)
You can just match for the negated regex and place the text then
您可以匹配否定的正则表达式然后放置文本