3

I have a string copied from a mutiline text box. I am looking for a method to replace the whole line which contains a specific phrase. For example the string looks like this:

Lorem ipsum dolor sit amet, 
consectetur adipiscing elit. 
Suspendisse egestas.

So I would like to find a method to replace the whole line that contains for example phrase elit with the new line enim vehicula pellentesque. so the resoult would be:

Lorem ipsum dolor sit amet, 
enim vehicula pellentesque. 
Suspendisse egestas.

Is there a quick way to do it?

Thanks

Devel
  • 950
  • 9
  • 17
  • 29
  • what do you mean by " Is there a quick way to do it " ? – Mitch Wheat Feb 05 '11 at 16:33
  • Please see http://www.regular-expressions.info/dotnet.html , a bit outdated but still very relevant site. Spend time there and you will be better programmer. And not just in C# ... –  Feb 05 '11 at 16:37
  • @mitch wheat: as opposed to slow way maybe lol :) – naveen Feb 05 '11 at 16:37
  • Its already there on [stackoverflow](http://stackoverflow.com/questions/2014671/delete-lines-from-beginning-of-multiline-textbox-in-c) – Harsh Baid Feb 06 '11 at 18:11

3 Answers3

9
var regex = new Regex(@"^.*\Welit\W.*$", RegexOptions.Multiline);
string result = regex.Replace(original, "enim vehicula pellentesque.");

RegexOptions.Multiline is key; it says to apply ^, ( = "beginning") and $ ( = "end") to mean beginning and end of line, instead of beginning and end of string.

The \Ws look for non-word characters on either side of elit, so e.g. fooelit will not match but foo elit will.

Domenic
  • 110,262
  • 41
  • 219
  • 271
8

If you're looking to replace all lines that contain that text, you could do:

textBox.Lines = textBox.Lines
                       .Select(line => line.Contains("elit") 
                               ? "enim vehicula pellentesque." : line)
                       .ToArray();

If you're dealing with just the string representing the lines, you could do something along the lines of:

string text = ...

var lines = from line in text.Split
                 (new[] { Environment.NewLine }, StringSplitOptions.None)
            select line.Contains("elit") ? "enim vehicula pellentesque." : line;

string replacedText = string.Join(Environment.NewLine, lines.ToArray());

EDIT: As JG points out in a comment, this won't work if you're looking for the specific word elit. In this case, you'll need a different predicate than a simple string.Contains. For example, you could just split the line by all whitespace characters and check if one of them is the blacklisted word:

line.Split().Contains("elit") // pseudo-overload of String.Split

You may need a fancier filter (Regex such as in Domenic's answer) depending on your definition of 'word'.

Ani
  • 111,048
  • 26
  • 262
  • 307
  • 2
    #wow i was writing the usual foreach.. A good use of LINQ i have ever seen – Shekhar_Pro Feb 05 '11 at 16:35
  • Though this will also erroneously replace a line containing `elite`, for example. – João Silva Feb 05 '11 at 16:39
  • @JG: very true. but have you noted that OP wanted to delete the line containing "elit." and not "elit"? – naveen Feb 05 '11 at 16:42
  • @yetanothercoder: Yes, but that is still the *word* `elit`. A simple way to detect it would be to use regex word boundaries (e.g., `@"\belit\b"`). Aside from this problem, which won't probably occur often if the OP is effectively dealing with latin phrases, this is a great answer and a *very* good use case for LINQ imho. – João Silva Feb 05 '11 at 16:51
  • @JG: Thanks for the `elite` comment; I've tried to address that with an edit. Not sure if that's what the OP really wants. – Ani Feb 05 '11 at 16:58
1
private void button1_Click(object sender, EventArgs e)
    {          
        foreach (var line in textBox1.Lines)
        {
            if (line.Contains("hello"))
            {
               textBox1.Text= textBox1.Text.Replace(line, "This is new line");
            }
        }
    }
Javed Akram
  • 15,024
  • 26
  • 81
  • 118