Hack Forums
[.NET] Very simple REGEX problem - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Very simple REGEX problem (/showthread.php?tid=5448298)



Very simple REGEX problem - WhatIsRS - Oct 29, 2016

Need help with this string.

Pretty much I need to detect if this exact string exists, however the numbers can be anything.

How can i do this using regex? Thanks

PHP Code

<?php 
string PageEnd 
= "{\"total\":335,\"items\":[]}"; 

Below doesn't work... but it's what im trying to do

PHP Code

<?php 
Regex r 
= new Regex("{\"total\":" + @"^\d+$" + ",\"items\":[]}");
if (
r.Match(stringImComparing).Success)
{
//do stuff
} 

Current work-around:
PHP Code

<?php 
string PageEnd 
= "items\":[]}";
if (
ReturnedHTML.Contains(PageEnd))
{
// Console.WriteLine("Found End Page");
MaxPage = k;
} 



* - -SomE. - Oct 29, 2016

Code
Try
    If Regex.IsMatch( "{\""total\"":335,\""items\"":[]}", "\{\\""total\\"":[^]\n\r"",:[\\eimst}]{3},\\""items\\"":\[\]\}") Then
        ' Successful match
    Else
        ' Match attempt failed
    End If
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try



* - WhatIsRS - Oct 29, 2016

(Oct 29, 2016 05:45 PM)-SomE. Wrote:  
Code
Try
    If Regex.IsMatch( "{\""total\"":335,\""items\"":[]}", "\{\\""total\\"":[^]\n\r"",:[\\eimst}]{3},\\""items\\"":\[\]\}") Then
        ' Successful match
    Else
        ' Match attempt failed
    End If
Catch ex As ArgumentException
    'Syntax error in the regular expression
End Try

Thanks 👍


* - +Haze - Oct 30, 2016

This isn't a task for regex mate, use a json deserializer to check or just use string#Contains to check if the two keys exists


* - Ninja - Oct 30, 2016

JSON deserialization would be much better. Add a reference to System.Web.Extensions and then you can use the System.Web.Script.Serialization.JavaScriptSerializer class.


* - mrkoolaid - Oct 30, 2016

pcre php regex

Code
([{].{2}total.{2}[:]\d{3}.{3}items.{3}[[][]][}])

however i do suggest deserialization as well :)


* - Zilog80 - Oct 30, 2016

(Oct 30, 2016 09:38 AM)Ninja Wrote:  JSON deserialization would be much better. Add a reference to System.Web.Extensions and then you can use the System.Web.Script.Serialization.JavaScriptSerializer class.

Sorry for the off-topic, but I have a question:

Are there any advantages referencing to System.Web.Extensions for using System.Web.Script.Serialization.JavaScriptSerializer instead adding a reference to System.Runtime.Serialization for using System.Runtime.Serialization.Json.DataContractJsonSerializer class?

Or maybe both classes 'do the same' job? (serialize/deserialize JSON data)

PS: I've always used DataContractJsonSerializer class for JSON stuff.


* - Ninja - Oct 31, 2016

(Oct 30, 2016 10:10 AM)Zilog80 Wrote:  Sorry for the off-topic, but I have a question:

Are there any advantages referencing to System.Web.Extensions for using System.Web.Script.Serialization.JavaScriptSerializer instead adding a reference to System.Runtime.Serialization for using System.Runtime.Serialization.Json.DataContractJsonSerializer class?

Or maybe both classes 'do the same' job? (serialize/deserialize JSON data)

PS: I've always used DataContractJsonSerializer class for JSON stuff.

I haven't used the latter that much and I am more familiar with the first. As far as advantages go I have no clue.