I have a variable of type string , there is a large text written there. I have a regular expression Regex regex = new Regex(@"<div class=\""b-opinions-main-2__text\"">.+</div>"); which is stored in a variable of type string . In place. + Should be text. How do I write. + To a variable?
- Either you are here msdn.microsoft.com/ru-ru/library/… , or you should rephrase the task - vitidev
- There is a line, the line contains the substrings '<div class = \ "" b-opinions-main-2__text \ ""> Hello World </ div> "' "> while World </ div> ', I made up the regular expression' <div class = \" "b-opinions-main-2__text \" ">. + </ div> ', how can I find all these substrings and write them to another variable so that I could get one of them from 'Hello World' or 'Bye Mir' - Pavel Kushnerevich
- Then I gave the correct link. - vitidev
2 answers
HTML needs to be parsed using appropriate tools .
Now essentially: that part of the regular expression that you need to get, you need to enclose in unshielded round brackets. (...) is an exciting group . There are named and numerical exciting groups. Here is an example of the latter: let's say you have your regulars, you need what you find .+ - enclose this part in brackets. In the code:
Regex reg = new Regex(@"<div class=\""b-opinions-main-2__text\"">(.+)</div>"); // ^^^^ var resultaty = reg.Matches(str) // Находим все совпадения .Cast<Match>() // Получаем список объектов Match .Select(m => m.Groups[1].Value) // Получаем нашу подстроку из группы №1 .ToList(); // Конвертируем в список. This regular pattern is taken from the vehicle only as a sample. In the real code I do not advise to use. Use the HtmlAgilityPack . Yes, you can try to improve it and even write @"(?s)<div\s+class=""b-opinions-main-2__text"">(.+?)</div>" , but this regular is also in one (un) beautiful moment may fail, because Regulars are not intended for parsing HTML.
You must use the Matches method to get all matches from the Regex class
string g = "<div class=\"b-opinions-main-2__text\">qwerty</div>"; var regExp = new Regex("(?<=<div class=\"b-opinions-main-2__text\">).+(?=</div>)"); var matches = regExp.Matches(g); The type of the matches variable is MatchCollection . This type does not allow the use of LINQ methods, but can be passed using a foreach .
foreach (var m in matches){ Console.WriteLine(m); } ps Either use @ , or escape quotes, but not all at once.
- This type does not allow the use of LINQ methods - it even allows. - Wiktor Stribiżew
- @ WiktorStribiżew if you make a cast - yes. Directly - no. - Vadim Prokopchuk