替换像x01这样的文本但不像\ x01 Regex C# [英] Replace Text like x01 but not like \x01 Regex C#

查看:103
本文介绍了替换像x01这样的文本但不像\ x01 Regex C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试替换文件中的文本 - 我知道我可以在给定目录的文件中搜索并读取所有文本 - 然后使用替换函数但是如何让正则表达式测试器替换typeA的文本但不是\typeA的文本,请注意typeA之前的反斜杠。有一个简单的方法来做到这一点?我知道我可以检查\typeA并且我知道我可以检查这个类型A和getmatches但是这里的旋转是如何使用\取消匹配的东西是typeA可以以,{或者什么都不作为前缀除了在所有情况下,我想从更改中排除\typeA。只是一个FYI \不是这些文件中的转义字符。

Hello I am trying to replace text in files - I know I can search with in files of a given directory and read all text - then use replace functions but how do I get the regular expression tester to replace text of typeA but not text of \typeA , please note the backslash before typeA . Is there a simple method to do this ? I know I can check for \typeA and I know I can check for this typeA and getmatches but the spin here for me is how to unmatch the ones with the \ the thing is typeA could be prefixed by ",{, or nothing at all but in all cases I would like to exclude \typeA from the change. Just an FYI the \ is not an escape character in these files.

推荐答案

你想使用负面的lookbehind。表达式
You want to use a negative lookbehind. Expression
(?<!\\)\b\w+\b

应匹配每个没有前缀的单词\

这就是你要照顾的吗?

should match each word not prefixed by \
Is that what you are looking after?


我刚用这个正则表达式测试器:http://www.carljohansen.co.uk/utils/ regexpdotnet.aspx



使用此正则表达式:[^ \\] typeA



违反原文发布它发现4个实例,而不是7它应该做你想要的。



^意味着没有,双反斜杠逃避反斜杠哟你不想要。



本质上它是说找一些不是反斜杠的东西,然后是typeA,那就是匹配。



我希望这会有所帮助。



Don



匹配#1 (char 213):typeA

匹配#2(字符277):typeA

匹配#3(字符385):typeA

匹配# 4(char 482):typeA

-------------------

您可以在这里尝试完整的解决方案: http://www.compileonline.com/compile_csharp_online.php



I just used this regex tester: http://www.carljohansen.co.uk/utils/regexpdotnet.aspx

Using this regex: [^\\]typeA

Against your original post it found 4 instances, not 7 It should do about what you want.

The ^ means not and the double backslash is escaping the backslash you do not want.

In essence it is saying look for something that is not an backslash followed by typeA and that is a match.

I hope this helps.

Don

Match #1 (char 213): typeA
Match #2 (char 277): typeA
Match #3 (char 385): typeA
Match #4 (char 482): typeA
-------------------
You can try the complete solution here: http://www.compileonline.com/compile_csharp_online.php

string source = @"Hello I am trying to replace text in files - I know I can search with in files of a given directory and read all text - then use replace functions but how do I get the regular expression tester to replace text of typeA but not text of \typeA , please note the backslash before typeA . Is there a simple method to do this ? I know I can check for \typeA and I know I can check for this typeA and getmatches but the spin here for me is how to unmatch the ones with the \ the thing is typeA could be prefixed by "",{ or nothing at all but in all cases I would like to exclude \typeA from the change. Just an FYI the \ is not an escape character in these files. ";
string match = @"([^\\])typeA";
string replace = @"


1typeB;
string target = Regex.Replace(source,match,replace);
Console.WriteLine(target);
1typeB"; string target = Regex.Replace(source, match, replace); Console.WriteLine(target);





只需将其粘贴在主要内容上,然后按编译和放大即可。执行



确保使用System.Text.RegularExpressions添加:



;



在顶部





Don



Just paste it in over what they have in main, and press "Compile & Execute"

Make sure you add:

using System.Text.RegularExpressions;

at the top


Don


这篇关于替换像x01这样的文本但不像\ x01 Regex C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆