Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted

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?
 
Share this answer
 
v2
Comments
stixoffire 21-May-14 9:52am    
Specifically the typeA is a prefix of a number of different words - but the words prefixed with \ is always the one that I do not want to change.
stixoffire 21-May-14 10:21am    
I think this is working (?<!\\)d01 where d01 is the literal text I am looking at and it can be d01xyzwhatever - I will try this out and see how well it does .. Thank you for this I had read about negation on a website but it was so poorly explained (even the examples) that I could not make heads or tales of it.
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

C#
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);


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
 
Share this answer
 
v3
Comments
stixoffire 21-May-14 10:15am    
This does eliminate the \typeA but it selects {typeA and I do want this selection excluding the { - so all typeA I want to be able to change but EXCLUDE it if it is preceded with the \ so I would select only typeA regardless of what precedes it EXCEPT and UNLESS the preceding character is \ in which case I do not want typeA at all.
ludemade 21-May-14 13:06pm    
I updated my response with the entire code you need to do the replace. Initially, I thought you just wanted the regex.
stixoffire 21-May-14 23:25pm    
The regex was fine but as I said there are conditions that it did not handle well - the negative look behind actually worked fine.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900