RegEx - убрать лишнее из строки - C#
Формулировка задачи:
Всем привет.
В файле есть текст примерно такой
Text1_Text2_Text3.Text4.Text5(Text6=Text7, Text8=Text9, Text10=Text11) Text12:Text13:Text14 {Text15}
Интересует выбрать
Text4, Text5, Text12, Text13, Text14, Text15
как задать шаблон?
пока удалось извлечь Text3 и Text4
Листинг программы
- const string PathFile = @"C:\File.txt";
- var sr = new StreamReader(PathFile);
- while (sr.Peek() >= 0)
- {
- string input = sr.ReadLine();
- string pattern = @"(\.)";
- string[] substring = Regex.Split(input, pattern);
- Text3 = substring[2].ToString();
- Text4 = substring[4].ToString().Split('(')[0];
- }
- sr.Close();
Решение задачи: «RegEx - убрать лишнее из строки»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- // обязательно это пространство имен подключать
- using System.Text.RegularExpressions;
- namespace ConsoleApplicaton9
- {
- class Program
- {
- static void Main (string[] args)
- {
- Console.WriteLine("Проверка на соответствие шаблону");
- Regex r = new Regex("собака", RegexOptions.IgnoreCase);
- string text1 = "Кот в доме, собака в конуре";
- string text2 = "Котик в доме, собачка в конуре";
- Console.WriteLine(r.IsMatch(text1));
- Console.WriteLine(r.IsMatch(text2));
- Console.WriteLine("Вывод на экран всех чисел, встречающихся в строчке");
- // Задаем искомую строчку
- string text = @"5*10=50-80/2=-2";
- Regex theReg = new Regex(@"[-+]?\d+");
- MatchCollection theMatches = theReg.Matches(text);
- foreach (Match theMatch in theMatches)
- {
- Console.Write("{0}", theMatch.ToString());
- }
- Console.WriteLine();
- Console.ReadLine();
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д