Search code examples
c#httpwebrequesthttpwebresponse

C# find string inside httpwebresponse


So i'm trying to find and grab a string from inside my httpwebresponse, I've already created a stream and can successfully read from the output to identify text inside the response, but now I'm trying to extract text from the response, for example:

Output contains,

    <script>
    var Site = 'home';

        var Home = {
            page: 24,
            name: 'Pokemon',
            Title: Pokemon chars
        };
     </script>

What I'm trying to do is extract only

 Pokemon

however this can change on various pages, so I can't just assume

name:

contains Pokemon

So what I need to do is trim name but keep the innertext, so what I've tried is something along the lines of

string str5 = "name: '";
foreach (string str6 in str3.Split(new char[] {'\n'}))  // str3 is = to the response given from the request which is now obviously a string its self.
{
   if (str6.StartsWith(str5))
   {
       str4 = str6.Replace(str5, "").Replace("',", "");
   }
}

Which should find the line containing " name: " then replace " name:" with nothing and the same for the end of that line leaving only the string

Pokemon

but it doesn't seem to be working?


Solution

  • One options is to use Regular Expression, if you cannot deserialize your JSON.

    string output = @"<script>
            var Site = 'home';
    
                var Home = {
                    page: 24,
                    name: 'Pokemon',
                    Title: Pokemon chars
                };
             </script>";
    
             string regexPattern = @"name:\s'(.+?)',";
             Regex reg = new Regex(regexPattern);
    
             var match = reg.Match(output);
    
             if (match.Success)
             {
             Console.WriteLine(match.Groups[1].Value);
             }
    

    Your are capturing everything between name: ' and ', using regular expression pattern.