sider med cirka en MASSE tekst i en enkelt stor tekst fil
jeg skal på en eller anden måde læse en hel linie ind og hvis det første tal efter en underscore er større end 10 og næste tal efter næste underscore i samme linie er større end 10 så skal den slette de næste 3 linier og erstatte disse linier med noget andet.
using System; using System.Text; using System.Text.RegularExpressions;
namespace E { public class Program { private static readonly Regex re = new Regex(@"(.*?_(\d+).*?_(\d+).*\r\n)(.*\r\n.*\r\n.*\r\n)", RegexOptions.Compiled); public static string XReplace(string s) { StringBuilder res = new StringBuilder(""); int ix = 0; foreach(Match m in re.Matches(s)) { res.Append(s.Substring(ix, m.Index - ix)); if(int.Parse(m.Groups[2].Value) > 10 && int.Parse(m.Groups[3].Value) > 10) { res.Append(m.Groups[1].Value); res.Append("x\r\nx\r\nx\r\n"); } else { res.Append(m.Groups[0].Value); } ix = m.Index + m.Length; } res.Append(s.Substring(ix)); return res.ToString(); } public static void Main(string[] args) { string s = @"a b_11 c_12 d e f g_9 h_10 i j k l m n_13 o p_14 q r s t u"; Console.WriteLine(s); Console.WriteLine(XReplace(s)); Console.ReadKey(); } } }
Synes godt om
Ny brugerNybegynder
Din løsning...
Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.