skip to Main Content

I’m facing a problem, I trying to select values of all title parameters which year is greater than 19999. I tried many ways. But it’s not work correctly, It shows all the year title.

In the below, this is my regex. But it’s not working

"Title":"([^"]+?)".*?"Year":20[0-9]{2}

My JSON data in the below

{
 "Data": {
   "Items": [
  {
    "Start": "2018-04-05T05:00:00Z",
    "End": "2018-04-05T05:30:00Z",
    "Title": "Indiana Jones and the Raiders of the Lost Ark",
    "Staff": {
      "Director": "Steven Spielberg",
      "Starring": "Harrison Ford"
    },
    "TimeZone": "UTC",
    "Id": "6ec29f07-c845-4967-9c87-716825265312",
    "Year": 1981,
    "IsNew": false,
    "ReadOnly": true
  },
  {
    "Start": "2018-06-29T12:00:00Z",
    "End": "2018-06-29T12:30:00Z",
    "Title": "The Lion King",
    "Staff": {
      "Director": "Roger Allers",
      "Starring": "James Earl Jones"
    },
    "TimeZone": "UTC",
    "Id": "9d54f04d-3554-4b61-8077-0cfb79cb86db",
    "Year": 1994,
    "IsNew": false,
    "ReadOnly": true
  },
  {
    "Start": "2018-06-01T08:00:00Z",
    "End": "2018-06-01T08:30:00Z",
    "Title": "Titanic ",
    "Staff": {
      "Director": "James Cameron",
      "Starring": "Leonardo DiCaprio"
    },
    "TimeZone": "UTC",
    "Id": "aa174cd4-da58-4bf9-a7a7-7ae562b584e6",
    "Year": 1997,
    "IsNew": false,
    "ReadOnly": true
  },
  {
    "Start": "2018-04-27T07:00:00Z",
    "End": "2018-04-27T07:30:00Z",
    "Title": "Ready Player One",
    "Staff": {
      "Director": "Steven Spielberg",
      "Starring": "Tye Sheridan"
    },
    "TimeZone": "UTC",
    "Id": "425b2969-8ec0-4943-93ce-c5a1aec148b0",
    "Year": 2018,
    "IsNew": true,
    "ReadOnly": false
  }
],
"From": 0,
"Portion": 0,
"TotalCount": 0
 },
  "RedirectViewModel": null,
 "ExceptionViewModel": null
 }

2

Answers


  1. Resolved through JavaScript Code:

    const js = {
    "Data": {
        "Items": [
            {
                "Start": "2018-04-05T05:00:00Z",
                "End": "2018-04-05T05:30:00Z",
                "Title": "Indiana Jones and the Raiders of the Lost Ark",
                "Staff": {
                    "Director": "Steven Spielberg",
                    "Starring": "Harrison Ford"
                },
                "TimeZone": "UTC",
                "Id": "6ec29f07-c845-4967-9c87-716825265312",
                "Year": 1981,
                "IsNew": false,
                "ReadOnly": true
            },
            {
                "Start": "2018-06-29T12:00:00Z",
                "End": "2018-06-29T12:30:00Z",
                "Title": "The Lion King",
                "Staff": {
                    "Director": "Roger Allers",
                    "Starring": "James Earl Jones"
                },
                "TimeZone": "UTC",
                "Id": "9d54f04d-3554-4b61-8077-0cfb79cb86db",
                "Year": 1994,
                "IsNew": false,
                "ReadOnly": true
            },
            {
                "Start": "2018-06-01T08:00:00Z",
                "End": "2018-06-01T08:30:00Z",
                "Title": "Titanic ",
                "Staff": {
                    "Director": "James Cameron",
                    "Starring": "Leonardo DiCaprio"
                },
                "TimeZone": "UTC",
                "Id": "aa174cd4-da58-4bf9-a7a7-7ae562b584e6",
                "Year": 1997,
                "IsNew": false,
                "ReadOnly": true
            },
            {
                "Start": "2018-04-27T07:00:00Z",
                "End": "2018-04-27T07:30:00Z",
                "Title": "Ready Player One",
                "Staff": {
                    "Director": "Steven Spielberg",
                    "Starring": "Tye Sheridan"
                },
                "TimeZone": "UTC",
                "Id": "425b2969-8ec0-4943-93ce-c5a1aec148b0",
                "Year": 2018,
                "IsNew": true,
                "ReadOnly": false
            }
        ],
        "From": 0,
        "Portion": 0,
        "TotalCount": 0
    },
    "RedirectViewModel": null,
    "ExceptionViewModel": null
    };
    
    // Select Titles after the Year
    const fileterData = js.Data.Items.filter(x => x.Year > 1999);
    console.log(fileterData.map(x => x.Title));
    
    // alternative: js.Data.Items.filter(x => x.Year > 1999).map(x => x.Title)
    Login or Signup to reply.
  2. Necessary disclaimer: this kind of task is best done with a proper JSON parser.

    Some issues in your current regex:

    • It requires there is no space following the colon that separates key and value.

    • It does not limit the search for an acceptable year to the object literal the title was found in.

    • It allows the year to have more than 4 digits.

    If we assume that the JSON structure is as depicted, including the order of the keys, and where nested objects will occur, we could first skip the nested object literal, i.e. the Staff key/value pair, and then look for the year, but not accepting any more closing braces.

    So:

    "Title":s*"([^"]+?)"[^}]*}[^}]*"Year":s*20[0-9]{2}b
    

    Note that your regex (and also this one) does not allow for empty titles, i.e. "", and requires that the second digit in the year is 0. This was not explicitly mentioned in the question, but I’ll assume that is what you intended.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search