[Solved] c# Regex catch string between two string [duplicate]


You can try this:
<[a-z\s]+id=[\'\"]mobile[\w]+[\'\"][\sa-zA-Z\d\'\=\;\:]*>([a-zA-Z\d\s]+)<[\/a-z\s]+>
Anyway it will not match special chars or symbols.
You can test and optimize it here: https://regex101.com/r/fnYQ1o/10

EDIT – Code example
This could be the portion of code to extract the messages:

 var rgx = @"<[a-z\s]+id=[\']mobile[\w]+[\'][\sa-zA-Z\d\s\'\=\;\:]*>([a-zA-Z\d\s]+)<[\/a-z\s]+>";
 var txt = "<!DOCTYPE html><html lang='it' xml:lang='it'><!-- <![endif]--><head><meta http-equiv='Content-Type' content="text/html; charset=UTF-8"><title>Banca Mediolanum S.p.A. | Accesso clienti</title><meta name="description" content="Banca Mediolanum S.p.A. | Accesso clienti"><meta name="keywords" content="Banca Mediolanum S.p.A. | Accesso clienti"><meta name="title" content="Banca Mediolanum S.p.A. | Accesso clienti"><meta name="author" content="Banca Mediolanum S.p.A."><meta name="robots" content="index, follow"><meta name="viewport" content="width=1439,user-scalable=no"><link rel="shortcut icon" href="https://stackoverflow.com/questions/46671610/./images/favicon.ico" type="image/x-icon"><style>#cort {background-image: url(bmedonline_10set.png);background-repeat: no-repeat;background-position-x: center;height: 850px;width: auto;/*background-size: 100%;*/}@media only screen and (max-width: 768px) and (min-width: 641px) section.contactus-area.chat {}body {border: 0 none;margin: 0;padding: 0}</style></head><body class=" "><!-- Google Tag Manager --><script>(function (w, d, s, l, i) {w[l] = w[l] || [];w[l].push({'gtm.start': new Date().getTime(),event: 'gtm.js'});var f = d.getElementsByTagName(s)[0],j = d.createElement(s),dl = l != 'dataLayer' ? '&l=" + l : "';j.async = true;j.src="https://www.googletagmanager.com/gtm.js?id=" + i + dl;f.parentNode.insertBefore(j, f);})(window, document, 'script', 'dataLayer', 'GTM-KGSP');</script><!-- End Google Tag Manager --><div id='cort'></div><div id='mobileTitle' style="display:none;">Titolo prova</div><div id='mobileBody' style="display:none;">Corpo messaggio prova</div></body></html>";

 /* Using matches and aggregation */
 var matches = Regex.Matches(txt, rgx).Cast<Match>();
 /* Aggregation without using foreach*/
 if (matches != null && matches.Count() > 0)
 {
    matches = matches.Where(x => !String.IsNullOrEmpty(x.Groups[1].Value));
    var exitString = matches.Select(x => x.Groups[1].Value).Aggregate((x, y) => x + "-" + y);
    Console.WriteLine("Match and aggregation");
    Console.WriteLine(exitString);
  }

  /* using replace with regex: .*<div id='mobileTitle'[\s\w\W]*>([\s\w]*)<\/div>[\s\r\n]*<div id='mobileBody'[\s\w\W]*>([\s\w]*)<\/div>.* */
  Console.WriteLine();
  Console.WriteLine(@"Replace with another regex");
  Console.WriteLine(Regex.Replace(txt, @".*<div id='mobileTitle'[\s\w\W]*>([\s\w]*)<\/div>[\s\r\n]*<div id='mobileBody'[\s\w\W]*>([\s\w]*)<\/div>.*", "$1-$2"));

  Console.ReadLine();

13

solved c# Regex catch string between two string [duplicate]