简体   繁体   English

C# 正则表达式替换忽略特定字符串

[英]C# Regex Replace ignore specific string

Since this is my first question here on stackoverflow I hope my question is correctly asked.由于这是我在 stackoverflow 上的第一个问题,我希望我的问题被正确提出。

Basicly I have a normal.txt file which contains any text like:基本上我有一个 normal.txt 文件,其中包含任何文本,例如:

car accident
people died
cat without owner


<!-- Text added at 6/29/2011 9:20:38 AM -->

Some addintional Text
other Text added
add Text

I have a write/append function which allows the user to append some text and set a little timestamp.我有一个写入/附加 function 允许用户 append 一些文本并设置一个小时间戳。

So my problem is: With another function, you can search and replace text in the textfile, but as you can guess if someone wants to replace the word "Text" it will be replaced in the xml-stylish comment(timestamp) as well.所以我的问题是:使用另一个 function,您可以搜索和替换文本文件中的文本,但您可以猜测是否有人想要替换“文本”这个词,它也会在 xml 样式的注释(时间戳)中被替换。

My result until now is到目前为止我的结果是

content = Regex.Replace(content,"[^<+.*"+input+".*>+]*", replace);
//content = content of the .txt file, input = search term, replace = string to replace

But this fails miserably, as some regex pro's will see without executing it.但这失败得很惨,因为一些正则表达式专业人士会在不执行它的情况下看到它。

Now I hope that some regex pro could help me out here and provide me a search pattern which replaces the normal text but ignores the timestamp.现在我希望一些正则表达式专业人士可以在这里帮助我,并为我提供一个替换普通文本但忽略时间戳的搜索模式。

I'm not realy aware of the logic from regex until now, nevertheless I understand the single expressions so this would be a hook for me to understand Regex more properly.直到现在我才真正意识到正则表达式的逻辑,但是我理解单个表达式,所以这将是我更正确地理解正则表达式的一个钩子。

Thanks in advice.谢谢指教。

If I understand your question correctly, you want to replace every instance of "Text" except for the one(s) inside the comment.如果我正确理解您的问题,您希望替换“文本”的每个实例,但评论中的实例除外。

The easist way is to use a negative lookbehind (fantastic description here ) as below:最简单的方法是使用否定的lookbehind(这里的描述很棒),如下所示:

content = Regex.Replace(content, @"(?<!<!--.*?)" + input, replace);

What you're doing is attempting to replace a repetition of any length of a character that is NOT <+.*> or a character contained in input with the value in replace .您正在做的是尝试用 replace 中的值replace非 <+.*> 或input中包含的字符的任意长度的重复。

If you're going to be working a lot with Regex, I would HIGHLY recommend giving the website above a good read.如果您要经常使用 Regex,我强烈建议您阅读上面的网站。 It's hands down the best intro to Regex that I've found, the time spent now will save you lots of headaches later!这是我发现的对 Regex 的最佳介绍,现在花费的时间将为您以后省去很多麻烦!

Edit编辑

Updated to add flexibility thanks to @stema感谢@stema 更新以增加灵活性

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

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