Just use a regular for
loop and you avoid the need for an extra list
for (var i=0; i<lines1.Count; i++)
{
lines1[i] = Regex.Replace(lines1[i],"bim(.*)","bom$1");
}
Note, however, that you are still creating a new string for every string in lines1
because string are immutable.
Or, if you want, you can just write an extension method, something like this should work:
public static class Extensions
{
public static IEnumerable<string> RegexReplace (this IEnumerable<string> strings, Regex regex, string replacement)
{
foreach (var s in strings)
{
yield return regex.Replace(s, replacement);
}
}
}
And you could call it like this:
var lines1 = File.ReadLines("in.txt").RegexReplace("bim(.*)","bom$1");
This extension would allow you to apply a regex to every string in a collection and since it's using deferred execution, it won't actually do anything until you iterate it. So, for example, if you only needed to check the first line (perhaps to decide if the rest of the file should be processed), you'd be able to shortcut out without looking at the rest of the lines. In a case like that, we can be O(1)
for best case.