3

I have a foreach statement that is searching for a string value within a List<string>. If the current line being read contains the string, I want to replace it, but with certain caveats.

foreach (string shorthandValue in shorthandFound)
{
    if (currentLine.Contains(shorthandValue))
    {
        // This method creates the new string that will replace the old one.
        string replaceText = CreateReplaceString(shorthandValue);
        string pattern = @"(?<!_)" + shorthandValue;
        Regex.Replace(currentLine, pattern, replaceText);
        // currentline is the line being read by the StreamReader.
     }
}

I'm trying to get the system to ignore the string if the shorthandValue is preceded by an underscore character ("_"). Otherwise, I want it to be replaced (even if it is at the start of the line).

What am I not doing correctly?

UPDATE

This is working mostly correctly:

Regex.Replace(currentFile, "[^_]" + Regex.Escape(shorthandValue), replaceText);

However, while it does ignore the underscore, it it removing any space before the shorthandValue string. So if the line read "This is a test123.", and the "test123" is replaced, I end up with this result:

"This is aVALUEOFTHESHORTHAND."

Why is the space being removed?

UPDATE AGAIN

I changed the regex back to my (?<!_) and it is preserving the spaces.

EustaceMonk
  • 305
  • 1
  • 5
  • 14
  • 2
    I **do** find that info site very helpful but to play with (most) regex in a live setting, try regexpal.com for practice. – BlackVegetable Jan 15 '13 at 18:14
  • regular-expressions.info is excellent! I get a feeling you just don't want to put in the time. Also, the regex you tried makes me wonder if by any chance you're working on your DIPS application..? – The Dag Jan 15 '13 at 18:16
  • It's not that I don't want to put in the time, I just haven't found that site to be that helpful in explaining things very clearly. I'm more of a book person, I guess. DIPS application? Not really sure what that is, so I don't think that's what I'm doing. – EustaceMonk Jan 15 '13 at 18:27
  • Are you throwing away the results of the Regex.Replace? ie maybe you just need `currentLine = Regex.Replace(currentLine, pattern, replaceText);` – sgmoore Jan 15 '13 at 18:31

2 Answers2

4

Your regex is correct. The problem is that Regex.Replace returns a new string.

You are ignoring the returned string.

James Kyburz
  • 13,775
  • 1
  • 32
  • 33
  • It should be noted that this requires a single non-underscore *before* `shorthandValue` and would not work if `String.StartsWith(shorthandValue) == true`. – user7116 Jan 15 '13 at 18:28
  • Your answer is what the OP already has, so I'm not sure what the correction was. – user7116 Jan 15 '13 at 18:29
2

Your regular expression looks correct, given you fix your code to actually save the string (hat tip to @jameskyburz), so you should still ensure that shorthandValue is treated as a literal. To accomplish this use Regex.Escape:

var pattern = String.Format(@"(?<!_){0}", Regex.Escape(shorthandValue))
Community
  • 1
  • 1
user7116
  • 63,008
  • 17
  • 141
  • 172