-2

I am trying to work on a regex that will take a string and divide it into different groups so that way I may call upon the groups as needed for my code. Below is the string I am attempting to make a pattern out of. This string comes from a database in SQL, however I am not sure if the following string is a JSON or not:

{"data":"","displayException":"Unable to authenticate user.  Please make sure you have an 
existing account.  If you do not, select Register to create an account.  Please contact 
1-234-567-8910 M-F 9a-5pm CT for further assistance.","exception":"UNABLE TO LOGIN",
"success":false}

Below is the pattern I am working on:

@"("(.*)"\:\"\"\,)(\"(.*)\"\:)(\"(.*)\")"

The problem I am having is that when it matches, I am getting multiple lines of the same thing. Below is a split list of how this is matching:

0. {
1. "data":"",
2. data
3. "displayException":"Unable to authenticate user. Please make sure you have an existing account. If you do not, select Register to create an account. Please contact 1-234-567-8910 M-F 9a-5pm CT for further assistance.","exception":
4. displayException":"Unable to authenticate user. Please make sure you have an existing account. If you do not, select Register to create an account. Please contact 1-234-567-8910 M-F 9a-5pm CT for further assistance.","exception
5. "UNABLE TO LOGIN","success"
6.  UNABLE TO LOGIN","success
7. :false}

What I want is essentially is one line similar to 4 but without the quotes in the middle, and one like like 6 but with "exception" in front not on the end of 4 and line 7 combined with 6 (if you want to add it great if not I can do that)

So my question is, in regex is their a way that I can delete those unnecessary lines, or is my regex just false and theirs a better one, or am I just have to force to call out those even numbers? Any help is appreciated!

AeroX
  • 3,387
  • 2
  • 25
  • 39
user4970927
  • 179
  • 8
  • 8
    You have JSON input data. Why don't you just use a JSON library like JSON.Net ? – thomasb Jul 31 '15 at 15:46
  • 1
    That incoming string looks like JSON. Why not use a JSON parser rather than a regex : https://msdn.microsoft.com/en-us/library/windows/apps/xaml/hh770289.aspx – Dave3of5 Jul 31 '15 at 15:47
  • 2
    possible duplicate of [Regular expression to parse an array of JSON objects?](http://stackoverflow.com/questions/408570/regular-expression-to-parse-an-array-of-json-objects) – Dave3of5 Jul 31 '15 at 15:49
  • @Dave3of5 I am not sure if this string is a JSON or not, it is coming from data that I have in SQL Server. Therefore I do not know if this data is JSON or not, so it cannot be a duplicate you suggested – user4970927 Jul 31 '15 at 15:52

1 Answers1

1

Well, you should parse your JSON !!

But, as the question was about regex, here is an attempt:

(?:(?:"([^"}]*)")|(\w+))\s*:\s*(?:(?:"([^"}]*)")|(\w+))

Regex live here.

  • Your regex works fine, however when I put it in visual studio it doesn't work. I think the problem is all of the quotes, so how do I go around that? – user4970927 Jul 31 '15 at 16:28
  • @user4970927. I didn't understand... are there others inputs that fails in this regex? –  Jul 31 '15 at 17:03
  • Other inputs? No. When I paste you line into my code in Visual Studio, there are red squiggles under the line because I think what is happening there are too many quotes. Just try to put it in Visual Studio or something in that nature – user4970927 Jul 31 '15 at 18:10
  • @user4970927. Sorry, I can't figure out the problem.. maybe someone else could help with this. –  Jul 31 '15 at 18:26